{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 03 - Graphical Causal Models\n",
    " \n",
    " \n",
    "\n",
    " \n",
    "## Thinking About Causality\n",
    " \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:27.723424Z",
     "start_time": "2023-06-25T15:09:25.235237Z"
    },
    "tags": [
     "hide-input",
     "hide-output"
    ]
   },
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import graphviz as gr\n",
    "from matplotlib import style\n",
    "import seaborn as sns\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "color=['0.3', '0.5', '0.7', '0.9']\n",
    "linestyle=['-', '--', ':', '-.']\n",
    "marker=['o', 'v', 'd', 'p']\n",
    "\n",
    "pd.set_option('display.max_rows', 6)\n",
    "\n",
    "gr.set_default_format(\"png\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:27.743557Z",
     "start_time": "2023-06-25T15:09:27.725297Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>gender</th>\n",
       "      <th>cross_sell_email</th>\n",
       "      <th>age</th>\n",
       "      <th>conversion</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>short</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>short</td>\n",
       "      <td>27</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>long</td>\n",
       "      <td>17</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>320</th>\n",
       "      <td>0</td>\n",
       "      <td>no_email</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>321</th>\n",
       "      <td>1</td>\n",
       "      <td>no_email</td>\n",
       "      <td>16</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>322</th>\n",
       "      <td>1</td>\n",
       "      <td>long</td>\n",
       "      <td>24</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>323 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     gender cross_sell_email  age  conversion\n",
       "0         0            short   15           0\n",
       "1         1            short   27           0\n",
       "2         1             long   17           0\n",
       "..      ...              ...  ...         ...\n",
       "320       0         no_email   15           0\n",
       "321       1         no_email   16           0\n",
       "322       1             long   24           1\n",
       "\n",
       "[323 rows x 4 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "data = pd.read_csv(\"./data/cross_sell_email.csv\")\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "### Visualizing Causal Relationships \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:28.364202Z",
     "start_time": "2023-06-25T15:09:27.744786Z"
    },
    "tags": [
     "hide-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"319pt\" height=\"188pt\"\n",
       " viewBox=\"0.00 0.00 319.00 188.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 184)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-184 315,-184 315,4 -4,4\"/>\n",
       "<!-- U -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>U</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"40.45\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"40.45\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">U</text>\n",
       "</g>\n",
       "<!-- conversion -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>conversion</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"79.45\" cy=\"-18\" rx=\"49.29\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"79.45\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">conversion</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;conversion -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>U&#45;&gt;conversion</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M26.85,-146.45C11.81,-128.52 -8.65,-97.31 4.45,-72 12.09,-57.25 25.93,-45.73 39.67,-37.25\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"41.68,-40.13 48.61,-32.11 38.19,-34.06 41.68,-40.13\"/>\n",
       "</g>\n",
       "<!-- age -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>age</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"40.45\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"40.45\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">age</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;age -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>U&#45;&gt;age</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M40.45,-143.7C40.45,-135.98 40.45,-126.71 40.45,-118.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"43.95,-118.1 40.45,-108.1 36.95,-118.1 43.95,-118.1\"/>\n",
       "</g>\n",
       "<!-- gender -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>gender</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"119.45\" cy=\"-90\" rx=\"34.39\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"119.45\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">gender</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;gender -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>U&#45;&gt;gender</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M56.06,-147.17C67.23,-137.27 82.49,-123.75 95.24,-112.46\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"97.62,-115.02 102.79,-105.77 92.98,-109.78 97.62,-115.02\"/>\n",
       "</g>\n",
       "<!-- age&#45;&gt;conversion -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>age&#45;&gt;conversion</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M49.5,-72.76C54.09,-64.53 59.78,-54.32 64.96,-45.02\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"68.1,-46.56 69.91,-36.12 61.99,-43.16 68.1,-46.56\"/>\n",
       "</g>\n",
       "<!-- gender&#45;&gt;conversion -->\n",
       "<g id=\"edge7\" class=\"edge\">\n",
       "<title>gender&#45;&gt;conversion</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M109.98,-72.41C105.26,-64.16 99.46,-54.01 94.18,-44.78\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"97.14,-42.9 89.14,-35.96 91.07,-46.38 97.14,-42.9\"/>\n",
       "</g>\n",
       "<!-- rnd -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>rnd</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"241.45\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"241.45\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">rnd</text>\n",
       "</g>\n",
       "<!-- cross_sell_email -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>cross_sell_email</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"241.45\" cy=\"-90\" rx=\"69.59\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"241.45\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">cross_sell_email</text>\n",
       "</g>\n",
       "<!-- rnd&#45;&gt;cross_sell_email -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>rnd&#45;&gt;cross_sell_email</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M241.45,-143.7C241.45,-135.98 241.45,-126.71 241.45,-118.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"244.95,-118.1 241.45,-108.1 237.95,-118.1 244.95,-118.1\"/>\n",
       "</g>\n",
       "<!-- cross_sell_email&#45;&gt;conversion -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>cross_sell_email&#45;&gt;conversion</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M207.1,-74.15C181.46,-63.07 146.35,-47.91 119.45,-36.28\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"120.51,-32.93 109.94,-32.17 117.73,-39.35 120.51,-32.93\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f5a99df10>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import graphviz as gr\n",
    "\n",
    "g_cross_sell = gr.Digraph()\n",
    "\n",
    "g_cross_sell.edge(\"U\", \"conversion\")\n",
    "g_cross_sell.edge(\"U\", \"age\")\n",
    "g_cross_sell.edge(\"U\", \"gender\")\n",
    "\n",
    "g_cross_sell.edge(\"rnd\", \"cross_sell_email\")\n",
    "g_cross_sell.edge(\"cross_sell_email\", \"conversion\")\n",
    "g_cross_sell.edge(\"age\", \"conversion\")\n",
    "g_cross_sell.edge(\"gender\", \"conversion\")\n",
    "\n",
    "g_cross_sell"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:28.575818Z",
     "start_time": "2023-06-25T15:09:28.367014Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"319pt\" height=\"188pt\"\n",
       " viewBox=\"0.00 0.00 319.00 188.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 184)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-184 315,-184 315,4 -4,4\"/>\n",
       "<!-- U -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>U</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"40.45\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"40.45\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">U</text>\n",
       "</g>\n",
       "<!-- conversion -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>conversion</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"79.45\" cy=\"-18\" rx=\"49.29\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"79.45\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">conversion</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;conversion -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>U&#45;&gt;conversion</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M26.85,-146.45C11.81,-128.52 -8.65,-97.31 4.45,-72 12.09,-57.25 25.93,-45.73 39.67,-37.25\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"41.68,-40.13 48.61,-32.11 38.19,-34.06 41.68,-40.13\"/>\n",
       "</g>\n",
       "<!-- age -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>age</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"40.45\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"40.45\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">age</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;age -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>U&#45;&gt;age</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M40.45,-143.7C40.45,-135.98 40.45,-126.71 40.45,-118.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"43.95,-118.1 40.45,-108.1 36.95,-118.1 43.95,-118.1\"/>\n",
       "</g>\n",
       "<!-- gender -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>gender</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"119.45\" cy=\"-90\" rx=\"34.39\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"119.45\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">gender</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;gender -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>U&#45;&gt;gender</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M56.06,-147.17C67.23,-137.27 82.49,-123.75 95.24,-112.46\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"97.62,-115.02 102.79,-105.77 92.98,-109.78 97.62,-115.02\"/>\n",
       "</g>\n",
       "<!-- age&#45;&gt;conversion -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>age&#45;&gt;conversion</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M49.5,-72.76C54.09,-64.53 59.78,-54.32 64.96,-45.02\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"68.1,-46.56 69.91,-36.12 61.99,-43.16 68.1,-46.56\"/>\n",
       "</g>\n",
       "<!-- gender&#45;&gt;conversion -->\n",
       "<g id=\"edge7\" class=\"edge\">\n",
       "<title>gender&#45;&gt;conversion</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M109.98,-72.41C105.26,-64.16 99.46,-54.01 94.18,-44.78\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"97.14,-42.9 89.14,-35.96 91.07,-46.38 97.14,-42.9\"/>\n",
       "</g>\n",
       "<!-- rnd -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>rnd</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"241.45\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"241.45\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">rnd</text>\n",
       "</g>\n",
       "<!-- cross_sell_email -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>cross_sell_email</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"241.45\" cy=\"-90\" rx=\"69.59\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"241.45\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">cross_sell_email</text>\n",
       "</g>\n",
       "<!-- rnd&#45;&gt;cross_sell_email -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>rnd&#45;&gt;cross_sell_email</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M241.45,-143.7C241.45,-135.98 241.45,-126.71 241.45,-118.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"244.95,-118.1 241.45,-108.1 237.95,-118.1 244.95,-118.1\"/>\n",
       "</g>\n",
       "<!-- cross_sell_email&#45;&gt;conversion -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>cross_sell_email&#45;&gt;conversion</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M207.1,-74.15C181.46,-63.07 146.35,-47.91 119.45,-36.28\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"120.51,-32.93 109.94,-32.17 117.73,-39.35 120.51,-32.93\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f5a9c5ad0>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g_cross_sell = gr.Digraph()\n",
    "\n",
    "g_cross_sell.edge(\"U\", \"conversion\")\n",
    "g_cross_sell.edge(\"U\", \"age\")\n",
    "g_cross_sell.edge(\"U\", \"gender\")\n",
    "\n",
    "g_cross_sell.edge(\"rnd\", \"cross_sell_email\")\n",
    "g_cross_sell.edge(\"cross_sell_email\", \"conversion\")\n",
    "g_cross_sell.edge(\"age\", \"conversion\")\n",
    "g_cross_sell.edge(\"gender\", \"conversion\")\n",
    "\n",
    "g_cross_sell"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:28.798849Z",
     "start_time": "2023-06-25T15:09:28.578426Z"
    },
    "tags": [
     "hide-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"372pt\" height=\"121pt\"\n",
       " viewBox=\"0.00 0.00 371.88 121.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 117)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-117 367.88,-117 367.88,4 -4,4\"/>\n",
       "<!-- U -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>U</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-95\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-91.3\" font-family=\"Times,serif\" font-size=\"14.00\">U</text>\n",
       "</g>\n",
       "<!-- conversion -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>conversion</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"314.48\" cy=\"-72\" rx=\"49.29\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"314.48\" y=\"-68.3\" font-family=\"Times,serif\" font-size=\"14.00\">conversion</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;conversion -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>U&#45;&gt;conversion</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M53.59,-98.34C91.98,-102.71 166.64,-108.75 229.09,-99 242.9,-96.85 257.53,-92.82 270.67,-88.52\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"272.05,-91.75 280.38,-85.21 269.79,-85.12 272.05,-91.75\"/>\n",
       "</g>\n",
       "<!-- X -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>X</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"159.54\" cy=\"-72\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"159.54\" y=\"-68.3\" font-family=\"Times,serif\" font-size=\"14.00\">X</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;X -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>U&#45;&gt;X</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M53.17,-90.57C73,-87.07 100.89,-82.16 122.99,-78.26\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"123.76,-81.68 133.01,-76.5 122.55,-74.79 123.76,-81.68\"/>\n",
       "</g>\n",
       "<!-- X&#45;&gt;conversion -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>X&#45;&gt;conversion</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M186.84,-72C205.51,-72 231.33,-72 254.79,-72\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"255.02,-75.5 265.02,-72 255.02,-68.5 255.02,-75.5\"/>\n",
       "</g>\n",
       "<!-- cross_sell_email -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>cross_sell_email</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"159.54\" cy=\"-18\" rx=\"69.59\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"159.54\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">cross_sell_email</text>\n",
       "</g>\n",
       "<!-- cross_sell_email&#45;&gt;conversion -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>cross_sell_email&#45;&gt;conversion</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M201.53,-32.47C222.32,-39.81 247.61,-48.74 268.89,-56.26\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"267.76,-59.57 278.36,-59.6 270.09,-52.97 267.76,-59.57\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f5a9c5b50>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# rankdir:LR layers the graph from left to right\n",
    "g_cross_sell = gr.Digraph(graph_attr={\"rankdir\": \"LR\"})\n",
    "\n",
    "g_cross_sell.edge(\"U\", \"conversion\")\n",
    "g_cross_sell.edge(\"U\", \"X\")\n",
    "\n",
    "g_cross_sell.edge(\"cross_sell_email\", \"conversion\")\n",
    "g_cross_sell.edge(\"X\", \"conversion\")\n",
    "\n",
    "g_cross_sell"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:29.004349Z",
     "start_time": "2023-06-25T15:09:28.801559Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"372pt\" height=\"121pt\"\n",
       " viewBox=\"0.00 0.00 371.88 121.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 117)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-117 367.88,-117 367.88,4 -4,4\"/>\n",
       "<!-- U -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>U</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-95\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-91.3\" font-family=\"Times,serif\" font-size=\"14.00\">U</text>\n",
       "</g>\n",
       "<!-- conversion -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>conversion</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"314.48\" cy=\"-72\" rx=\"49.29\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"314.48\" y=\"-68.3\" font-family=\"Times,serif\" font-size=\"14.00\">conversion</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;conversion -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>U&#45;&gt;conversion</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M53.59,-98.34C91.98,-102.71 166.64,-108.75 229.09,-99 242.9,-96.85 257.53,-92.82 270.67,-88.52\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"272.05,-91.75 280.38,-85.21 269.79,-85.12 272.05,-91.75\"/>\n",
       "</g>\n",
       "<!-- X -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>X</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"159.54\" cy=\"-72\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"159.54\" y=\"-68.3\" font-family=\"Times,serif\" font-size=\"14.00\">X</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;X -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>U&#45;&gt;X</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M53.17,-90.57C73,-87.07 100.89,-82.16 122.99,-78.26\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"123.76,-81.68 133.01,-76.5 122.55,-74.79 123.76,-81.68\"/>\n",
       "</g>\n",
       "<!-- X&#45;&gt;conversion -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>X&#45;&gt;conversion</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M186.84,-72C205.51,-72 231.33,-72 254.79,-72\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"255.02,-75.5 265.02,-72 255.02,-68.5 255.02,-75.5\"/>\n",
       "</g>\n",
       "<!-- cross_sell_email -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>cross_sell_email</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"159.54\" cy=\"-18\" rx=\"69.59\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"159.54\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">cross_sell_email</text>\n",
       "</g>\n",
       "<!-- cross_sell_email&#45;&gt;conversion -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>cross_sell_email&#45;&gt;conversion</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M201.53,-32.47C222.32,-39.81 247.61,-48.74 268.89,-56.26\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"267.76,-59.57 278.36,-59.6 270.09,-52.97 267.76,-59.57\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f5a9d3710>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g_cross_sell = gr.Digraph(graph_attr={\"rankdir\": \"LR\"})\n",
    "\n",
    "g_cross_sell.edge(\"U\", \"conversion\")\n",
    "g_cross_sell.edge(\"U\", \"X\")\n",
    "\n",
    "g_cross_sell.edge(\"cross_sell_email\", \"conversion\")\n",
    "g_cross_sell.edge(\"X\", \"conversion\")\n",
    "\n",
    "g_cross_sell"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " \n",
    "### Are Consultants Worth it?\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:29.204706Z",
     "start_time": "2023-06-25T15:09:29.006360Z"
    },
    "tags": [
     "hide-input",
     "hide-code"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"553pt\" height=\"144pt\"\n",
       " viewBox=\"0.00 0.00 553.47 144.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 140)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-140 549.47,-140 549.47,4 -4,4\"/>\n",
       "<!-- U1 -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>U1</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"317.73\" cy=\"-118\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"317.73\" y=\"-114.3\" font-family=\"Times,serif\" font-size=\"14.00\">U1</text>\n",
       "</g>\n",
       "<!-- profits_next_6m -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>profits_next_6m</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"475.92\" cy=\"-64\" rx=\"69.59\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"475.92\" y=\"-60.3\" font-family=\"Times,serif\" font-size=\"14.00\">profits_next_6m</text>\n",
       "</g>\n",
       "<!-- U1&#45;&gt;profits_next_6m -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>U1&#45;&gt;profits_next_6m</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M342.12,-109.92C363.66,-102.47 396.37,-91.16 423.93,-81.63\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"425.13,-84.92 433.44,-78.34 422.84,-78.3 425.13,-84.92\"/>\n",
       "</g>\n",
       "<!-- U2 -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>U2</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"159.54\" cy=\"-72\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"159.54\" y=\"-68.3\" font-family=\"Times,serif\" font-size=\"14.00\">U2</text>\n",
       "</g>\n",
       "<!-- consultancy -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>consultancy</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"317.73\" cy=\"-64\" rx=\"52.79\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"317.73\" y=\"-60.3\" font-family=\"Times,serif\" font-size=\"14.00\">consultancy</text>\n",
       "</g>\n",
       "<!-- U2&#45;&gt;consultancy -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>U2&#45;&gt;consultancy</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M186.69,-70.66C205.46,-69.7 231.55,-68.36 255.44,-67.14\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"255.71,-70.63 265.52,-66.62 255.35,-63.64 255.71,-70.63\"/>\n",
       "</g>\n",
       "<!-- consultancy&#45;&gt;profits_next_6m -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>consultancy&#45;&gt;profits_next_6m</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M370.74,-64C378.91,-64 387.5,-64 396.11,-64\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"396.19,-67.5 406.19,-64 396.19,-60.5 396.19,-67.5\"/>\n",
       "</g>\n",
       "<!-- U3 -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>U3</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">U3</text>\n",
       "</g>\n",
       "<!-- profits_prev_6m -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>profits_prev_6m</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"159.54\" cy=\"-18\" rx=\"69.59\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"159.54\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">profits_prev_6m</text>\n",
       "</g>\n",
       "<!-- U3&#45;&gt;profits_prev_6m -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>U3&#45;&gt;profits_prev_6m</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M54.1,-18C61.75,-18 70.54,-18 79.7,-18\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"79.76,-21.5 89.76,-18 79.76,-14.5 79.76,-21.5\"/>\n",
       "</g>\n",
       "<!-- profits_prev_6m&#45;&gt;profits_next_6m -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>profits_prev_6m&#45;&gt;profits_next_6m</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M228,-21.32C269.32,-24.04 323.16,-28.82 370.38,-37 386.44,-39.78 403.65,-43.79 419.38,-47.87\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"418.91,-51.36 429.47,-50.55 420.7,-44.6 418.91,-51.36\"/>\n",
       "</g>\n",
       "<!-- profits_prev_6m&#45;&gt;consultancy -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>profits_prev_6m&#45;&gt;consultancy</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M206.14,-31.43C225.4,-37.1 247.86,-43.72 267.5,-49.5\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"266.59,-52.88 277.18,-52.35 268.57,-46.17 266.59,-52.88\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f5a9de4d0>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g_consultancy = gr.Digraph(graph_attr={\"rankdir\": \"LR\"})\n",
    "\n",
    "g_consultancy.edge(\"U1\", \"profits_next_6m\")\n",
    "g_consultancy.edge(\"U2\", \"consultancy\")\n",
    "g_consultancy.edge(\"U3\", \"profits_prev_6m\")\n",
    "\n",
    "g_consultancy.edge(\"consultancy\", \"profits_next_6m\")\n",
    "\n",
    "g_consultancy.edge(\"profits_prev_6m\", \"consultancy\")\n",
    "g_consultancy.edge(\"profits_prev_6m\", \"profits_next_6m\")\n",
    "\n",
    "g_consultancy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:29.429558Z",
     "start_time": "2023-06-25T15:09:29.207127Z"
    },
    "tags": [
     "hide-input",
     "hide-code"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"463pt\" height=\"67pt\"\n",
       " viewBox=\"0.00 0.00 463.47 67.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 63)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-63 459.47,-63 459.47,4 -4,4\"/>\n",
       "<!-- consultancy -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>consultancy</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"227.73\" cy=\"-41\" rx=\"52.79\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"227.73\" y=\"-37.3\" font-family=\"Times,serif\" font-size=\"14.00\">consultancy</text>\n",
       "</g>\n",
       "<!-- profits_next_6m -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>profits_next_6m</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"385.92\" cy=\"-18\" rx=\"69.59\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"385.92\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">profits_next_6m</text>\n",
       "</g>\n",
       "<!-- consultancy&#45;&gt;profits_next_6m -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>consultancy&#45;&gt;profits_next_6m</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M276.44,-33.97C288.67,-32.17 302.07,-30.2 315.16,-28.27\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"315.69,-31.73 325.08,-26.81 314.67,-24.81 315.69,-31.73\"/>\n",
       "</g>\n",
       "<!-- profits_prev_6m -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>profits_prev_6m</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"69.54\" cy=\"-18\" rx=\"69.59\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"69.54\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">profits_prev_6m</text>\n",
       "</g>\n",
       "<!-- profits_prev_6m&#45;&gt;consultancy -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>profits_prev_6m&#45;&gt;consultancy</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M130.48,-26.83C142.98,-28.67 156.14,-30.61 168.58,-32.44\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"168.42,-35.95 178.82,-33.94 169.44,-29.03 168.42,-35.95\"/>\n",
       "</g>\n",
       "<!-- profits_prev_6m&#45;&gt;profits_next_6m -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>profits_prev_6m&#45;&gt;profits_next_6m</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M138.3,-15.03C150.54,-14.6 163.19,-14.23 175.09,-14 221.87,-13.11 233.59,-13.11 280.38,-14 289.02,-14.17 298.06,-14.41 307.06,-14.69\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"307.06,-18.19 317.17,-15.03 307.29,-11.2 307.06,-18.19\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f5a9de490>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g_consultancy = gr.Digraph(graph_attr={\"rankdir\":\"LR\"})\n",
    "\n",
    "g_consultancy.edge(\"consultancy\", \"profits_next_6m\")\n",
    "g_consultancy.edge(\"profits_prev_6m\", \"consultancy\")\n",
    "g_consultancy.edge(\"profits_prev_6m\", \"profits_next_6m\")\n",
    "\n",
    "g_consultancy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Crash Course in Graphical Models\n",
    " \n",
    " \n",
    "### Chains\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:29.656253Z",
     "start_time": "2023-06-25T15:09:29.431670Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"480pt\" height=\"98pt\"\n",
       " viewBox=\"0.00 0.00 480.36 98.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 94)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-94 476.36,-94 476.36,4 -4,4\"/>\n",
       "<!-- T -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>T</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"73.44\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"73.44\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">T</text>\n",
       "</g>\n",
       "<!-- M -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>M</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"247.88\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"247.88\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">M</text>\n",
       "</g>\n",
       "<!-- T&#45;&gt;M -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>T&#45;&gt;M</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M100.63,-18C129.96,-18 177.69,-18 210.61,-18\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"210.73,-21.5 220.73,-18 210.73,-14.5 210.73,-21.5\"/>\n",
       "</g>\n",
       "<!-- Y -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"410.62\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"410.62\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- M&#45;&gt;Y -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>M&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M275.06,-18C301.8,-18 343.52,-18 373.41,-18\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"373.45,-21.5 383.45,-18 373.45,-14.5 373.45,-21.5\"/>\n",
       "</g>\n",
       "<!-- causal knowledge -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>causal knowledge</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"73.44\" cy=\"-72\" rx=\"73.39\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"73.44\" y=\"-68.3\" font-family=\"Times,serif\" font-size=\"14.00\">causal knowledge</text>\n",
       "</g>\n",
       "<!-- solve problems -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>solve problems</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"247.88\" cy=\"-72\" rx=\"64.99\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"247.88\" y=\"-68.3\" font-family=\"Times,serif\" font-size=\"14.00\">solve problems</text>\n",
       "</g>\n",
       "<!-- causal knowledge&#45;&gt;solve problems -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>causal knowledge&#45;&gt;solve problems</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M147.07,-72C155.51,-72 164.13,-72 172.6,-72\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"172.82,-75.5 182.82,-72 172.82,-68.5 172.82,-75.5\"/>\n",
       "</g>\n",
       "<!-- job promotion -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>job promotion</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"410.62\" cy=\"-72\" rx=\"61.99\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"410.62\" y=\"-68.3\" font-family=\"Times,serif\" font-size=\"14.00\">job promotion</text>\n",
       "</g>\n",
       "<!-- solve problems&#45;&gt;job promotion -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>solve problems&#45;&gt;job promotion</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M313.33,-72C321.65,-72 330.21,-72 338.63,-72\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"338.81,-75.5 348.8,-72 338.8,-68.5 338.81,-75.5\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f5a9e1290>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph(graph_attr={\"rankdir\": \"LR\"})\n",
    "\n",
    "g.edge(\"T\", \"M\")\n",
    "g.edge(\"M\", \"Y\")\n",
    "g.node(\"M\", \"M\")\n",
    "\n",
    "\n",
    "g.edge(\"causal knowledge\", \"solve problems\")\n",
    "g.edge(\"solve problems\", \"job promotion\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:29.870382Z",
     "start_time": "2023-06-25T15:09:29.661788Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"480pt\" height=\"98pt\"\n",
       " viewBox=\"0.00 0.00 480.36 98.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 94)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-94 476.36,-94 476.36,4 -4,4\"/>\n",
       "<!-- T -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>T</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"73.44\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"73.44\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">T</text>\n",
       "</g>\n",
       "<!-- M -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>M</title>\n",
       "<ellipse fill=\"lightgrey\" stroke=\"lightgrey\" cx=\"247.88\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"247.88\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">M</text>\n",
       "</g>\n",
       "<!-- T&#45;&gt;M -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>T&#45;&gt;M</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M100.63,-18C129.96,-18 177.69,-18 210.61,-18\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"210.73,-21.5 220.73,-18 210.73,-14.5 210.73,-21.5\"/>\n",
       "</g>\n",
       "<!-- Y -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"410.62\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"410.62\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- M&#45;&gt;Y -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>M&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M275.06,-18C301.8,-18 343.52,-18 373.41,-18\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"373.45,-21.5 383.45,-18 373.45,-14.5 373.45,-21.5\"/>\n",
       "</g>\n",
       "<!-- causal knowledge -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>causal knowledge</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"73.44\" cy=\"-72\" rx=\"73.39\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"73.44\" y=\"-68.3\" font-family=\"Times,serif\" font-size=\"14.00\">causal knowledge</text>\n",
       "</g>\n",
       "<!-- solve problems -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>solve problems</title>\n",
       "<ellipse fill=\"lightgrey\" stroke=\"lightgrey\" cx=\"247.88\" cy=\"-72\" rx=\"64.99\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"247.88\" y=\"-68.3\" font-family=\"Times,serif\" font-size=\"14.00\">solve problems</text>\n",
       "</g>\n",
       "<!-- causal knowledge&#45;&gt;solve problems -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>causal knowledge&#45;&gt;solve problems</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M147.07,-72C155.51,-72 164.13,-72 172.6,-72\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"172.82,-75.5 182.82,-72 172.82,-68.5 172.82,-75.5\"/>\n",
       "</g>\n",
       "<!-- job promotion -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>job promotion</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"410.62\" cy=\"-72\" rx=\"61.99\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"410.62\" y=\"-68.3\" font-family=\"Times,serif\" font-size=\"14.00\">job promotion</text>\n",
       "</g>\n",
       "<!-- solve problems&#45;&gt;job promotion -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>solve problems&#45;&gt;job promotion</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M313.33,-72C321.65,-72 330.21,-72 338.63,-72\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"338.81,-75.5 348.8,-72 338.8,-68.5 338.81,-75.5\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f5a9d3ed0>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph(graph_attr={\"rankdir\": \"LR\"})\n",
    "\n",
    "g.edge(\"T\", \"M\")\n",
    "g.edge(\"M\", \"Y\")\n",
    "g.node(\"M\", \"M\")\n",
    "g.node(\"M\", color=\"lightgrey\", style=\"filled\")\n",
    "\n",
    "\n",
    "g.edge(\"causal knowledge\", \"solve problems\")\n",
    "g.edge(\"solve problems\", \"job promotion\")\n",
    "g.node(\"solve problems\", color=\"lightgrey\", style=\"filled\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "### Forks\n",
    " \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:30.108147Z",
     "start_time": "2023-06-25T15:09:29.872267Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"450pt\" height=\"116pt\"\n",
       " viewBox=\"0.00 0.00 450.14 116.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 112)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-112 446.14,-112 446.14,4 -4,4\"/>\n",
       "<!-- X -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>X</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"63\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"63\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">X</text>\n",
       "</g>\n",
       "<!-- Y -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- X&#45;&gt;Y -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>X&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M54.65,-72.76C50.29,-64.28 44.85,-53.71 39.96,-44.2\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"42.99,-42.44 35.3,-35.15 36.77,-45.64 42.99,-42.44\"/>\n",
       "</g>\n",
       "<!-- T -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>T</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"99\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"99\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">T</text>\n",
       "</g>\n",
       "<!-- X&#45;&gt;T -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>X&#45;&gt;T</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M71.35,-72.76C75.71,-64.28 81.15,-53.71 86.04,-44.2\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"89.23,-45.64 90.7,-35.15 83.01,-42.44 89.23,-45.64\"/>\n",
       "</g>\n",
       "<!-- statistics -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>statistics</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"291\" cy=\"-90\" rx=\"40.89\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"291\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">statistics</text>\n",
       "</g>\n",
       "<!-- causal inference -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>causal inference</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"212\" cy=\"-18\" rx=\"67.69\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"212\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">causal inference</text>\n",
       "</g>\n",
       "<!-- statistics&#45;&gt;causal inference -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>statistics&#45;&gt;causal inference</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M273.47,-73.46C263.13,-64.3 249.84,-52.53 238.27,-42.27\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"240.39,-39.48 230.58,-35.47 235.75,-44.72 240.39,-39.48\"/>\n",
       "</g>\n",
       "<!-- machine learning -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>machine learning</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"370\" cy=\"-18\" rx=\"72.29\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"370\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">machine learning</text>\n",
       "</g>\n",
       "<!-- statistics&#45;&gt;machine learning -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>statistics&#45;&gt;machine learning</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M308.53,-73.46C318.87,-64.3 332.16,-52.53 343.73,-42.27\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"346.25,-44.72 351.42,-35.47 341.61,-39.48 346.25,-44.72\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f5a9d3390>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph()\n",
    "\n",
    "\n",
    "g.edge(\"X\", \"Y\")\n",
    "g.edge(\"X\", \"T\")\n",
    "g.node(\"X\", \"X\")\n",
    "\n",
    "g.edge(\"statistics\", \"causal inference\")\n",
    "g.edge(\"statistics\", \"machine learning\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:30.323535Z",
     "start_time": "2023-06-25T15:09:30.110294Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"344pt\" height=\"116pt\"\n",
       " viewBox=\"0.00 0.00 343.59 116.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 112)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-112 339.59,-112 339.59,4 -4,4\"/>\n",
       "<!-- good programmer -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>good programmer</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"180.94\" cy=\"-90\" rx=\"75.29\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"180.94\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">good programmer</text>\n",
       "</g>\n",
       "<!-- can invert a binary tree -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>can invert a binary tree</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"92.94\" cy=\"-18\" rx=\"92.88\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"92.94\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">can invert a binary tree</text>\n",
       "</g>\n",
       "<!-- good programmer&#45;&gt;can invert a binary tree -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>good programmer&#45;&gt;can invert a binary tree</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M160.09,-72.41C148.66,-63.32 134.33,-51.92 121.84,-41.99\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"123.96,-39.2 113.95,-35.71 119.6,-44.68 123.96,-39.2\"/>\n",
       "</g>\n",
       "<!-- good employee -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>good employee</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"269.94\" cy=\"-18\" rx=\"65.79\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"269.94\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">good employee</text>\n",
       "</g>\n",
       "<!-- good programmer&#45;&gt;good employee -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>good programmer&#45;&gt;good employee</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M202.03,-72.41C213.76,-63.19 228.51,-51.59 241.27,-41.55\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"243.61,-44.16 249.31,-35.23 239.28,-38.66 243.61,-44.16\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f5a9d3050>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph()\n",
    "\n",
    "g.edge(\"good programmer\", \"can invert a binary tree\")\n",
    "g.edge(\"good programmer\", \"good employee\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    " \n",
    "### Immorality or Collider \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:30.517990Z",
     "start_time": "2023-06-25T15:09:30.326021Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"326pt\" height=\"116pt\"\n",
       " viewBox=\"0.00 0.00 325.85 116.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 112)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-112 321.85,-112 321.85,4 -4,4\"/>\n",
       "<!-- Y -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- X -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>X</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"78\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"78\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">X</text>\n",
       "</g>\n",
       "<!-- Y&#45;&gt;X -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>Y&#45;&gt;X</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M38.32,-73.46C44.92,-64.4 53.38,-52.79 60.8,-42.61\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"63.65,-44.64 66.71,-34.49 58,-40.51 63.65,-44.64\"/>\n",
       "</g>\n",
       "<!-- T -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>T</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"99\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"99\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">T</text>\n",
       "</g>\n",
       "<!-- T&#45;&gt;X -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>T&#45;&gt;X</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M93.92,-72.05C91.52,-64.06 88.6,-54.33 85.92,-45.4\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"89.26,-44.36 83.04,-35.79 82.56,-46.37 89.26,-44.36\"/>\n",
       "</g>\n",
       "<!-- statistics -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>statistics</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"195\" cy=\"-90\" rx=\"40.89\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"195\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">statistics</text>\n",
       "</g>\n",
       "<!-- job promotion -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>job promotion</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"215\" cy=\"-18\" rx=\"61.99\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"215\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">job promotion</text>\n",
       "</g>\n",
       "<!-- statistics&#45;&gt;job promotion -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>statistics&#45;&gt;job promotion</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M199.84,-72.05C202.07,-64.26 204.77,-54.82 207.26,-46.08\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"210.68,-46.85 210.06,-36.28 203.95,-44.93 210.68,-46.85\"/>\n",
       "</g>\n",
       "<!-- flatter -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>flatter</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"286\" cy=\"-90\" rx=\"31.7\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"286\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">flatter</text>\n",
       "</g>\n",
       "<!-- flatter&#45;&gt;job promotion -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>flatter&#45;&gt;job promotion</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M270.59,-73.81C261.34,-64.69 249.37,-52.88 238.91,-42.57\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"241.19,-39.91 231.61,-35.38 236.28,-44.89 241.19,-39.91\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f5a9e1a50>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph()\n",
    "\n",
    "g.edge(\"Y\", \"X\")\n",
    "g.edge(\"T\", \"X\")\n",
    "\n",
    "g.edge(\"statistics\", \"job promotion\")\n",
    "g.edge(\"flatter\", \"job promotion\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:30.744680Z",
     "start_time": "2023-06-25T15:09:30.519766Z"
    },
    "scrolled": true,
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"326pt\" height=\"188pt\"\n",
       " viewBox=\"0.00 0.00 325.85 188.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 184)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-184 321.85,-184 321.85,4 -4,4\"/>\n",
       "<!-- Y -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- X1 -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>X1</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"78\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"78\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">X1</text>\n",
       "</g>\n",
       "<!-- Y&#45;&gt;X1 -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>Y&#45;&gt;X1</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M38.32,-145.46C44.92,-136.4 53.38,-124.79 60.8,-114.61\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"63.65,-116.64 66.71,-106.49 58,-112.51 63.65,-116.64\"/>\n",
       "</g>\n",
       "<!-- X2 -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>X2</title>\n",
       "<ellipse fill=\"lightgrey\" stroke=\"lightgrey\" cx=\"78\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"78\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">X2</text>\n",
       "</g>\n",
       "<!-- X1&#45;&gt;X2 -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>X1&#45;&gt;X2</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M78,-71.7C78,-63.98 78,-54.71 78,-46.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"81.5,-46.1 78,-36.1 74.5,-46.1 81.5,-46.1\"/>\n",
       "</g>\n",
       "<!-- T -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>T</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"99\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"99\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">T</text>\n",
       "</g>\n",
       "<!-- T&#45;&gt;X1 -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>T&#45;&gt;X1</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M93.92,-144.05C91.52,-136.06 88.6,-126.33 85.92,-117.4\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"89.26,-116.36 83.04,-107.79 82.56,-118.37 89.26,-116.36\"/>\n",
       "</g>\n",
       "<!-- statistics -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>statistics</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"195\" cy=\"-162\" rx=\"40.89\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"195\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">statistics</text>\n",
       "</g>\n",
       "<!-- job promotion -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>job promotion</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"215\" cy=\"-90\" rx=\"61.99\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"215\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">job promotion</text>\n",
       "</g>\n",
       "<!-- statistics&#45;&gt;job promotion -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>statistics&#45;&gt;job promotion</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M199.84,-144.05C202.07,-136.26 204.77,-126.82 207.26,-118.08\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"210.68,-118.85 210.06,-108.28 203.95,-116.93 210.68,-118.85\"/>\n",
       "</g>\n",
       "<!-- high salary -->\n",
       "<g id=\"node8\" class=\"node\">\n",
       "<title>high salary</title>\n",
       "<ellipse fill=\"lightgrey\" stroke=\"lightgrey\" cx=\"215\" cy=\"-18\" rx=\"50.09\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"215\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">high salary</text>\n",
       "</g>\n",
       "<!-- job promotion&#45;&gt;high salary -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>job promotion&#45;&gt;high salary</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M215,-71.7C215,-63.98 215,-54.71 215,-46.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"218.5,-46.1 215,-36.1 211.5,-46.1 218.5,-46.1\"/>\n",
       "</g>\n",
       "<!-- flatter -->\n",
       "<g id=\"node7\" class=\"node\">\n",
       "<title>flatter</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"286\" cy=\"-162\" rx=\"31.7\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"286\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">flatter</text>\n",
       "</g>\n",
       "<!-- flatter&#45;&gt;job promotion -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>flatter&#45;&gt;job promotion</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M270.59,-145.81C261.34,-136.69 249.37,-124.88 238.91,-114.57\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"241.19,-111.91 231.61,-107.38 236.28,-116.89 241.19,-111.91\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f5a9f00d0>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph()\n",
    "\n",
    "g.edge(\"Y\", \"X1\")\n",
    "g.edge(\"T\", \"X1\")\n",
    "g.edge(\"X1\", \"X2\")\n",
    "g.node(\"X2\", color=\"lightgrey\", style=\"filled\")\n",
    "\n",
    "g.edge(\"statistics\", \"job promotion\")\n",
    "g.edge(\"flatter\", \"job promotion\")\n",
    "g.edge(\"job promotion\", \"high salary\")\n",
    "\n",
    "g.node(\"high salary\", color=\"lightgrey\", style=\"filled\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### The Flow of Association Cheat Sheet\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Querying a Graph in Python\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:30.952339Z",
     "start_time": "2023-06-25T15:09:30.746564Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"242pt\" height=\"152pt\"\n",
       " viewBox=\"0.00 0.00 242.00 152.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 148)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-148 238,-148 238,4 -4,4\"/>\n",
       "<!-- C -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>C</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-72\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-68.3\" font-family=\"Times,serif\" font-size=\"14.00\">C</text>\n",
       "</g>\n",
       "<!-- A -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>A</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"117\" cy=\"-126\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"117\" y=\"-122.3\" font-family=\"Times,serif\" font-size=\"14.00\">A</text>\n",
       "</g>\n",
       "<!-- C&#45;&gt;A -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>C&#45;&gt;A</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M47.53,-83.98C59.38,-91.26 74.7,-100.66 87.84,-108.72\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"86.17,-111.8 96.52,-114.05 89.83,-105.84 86.17,-111.8\"/>\n",
       "</g>\n",
       "<!-- B -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>B</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"117\" cy=\"-72\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"117\" y=\"-68.3\" font-family=\"Times,serif\" font-size=\"14.00\">B</text>\n",
       "</g>\n",
       "<!-- C&#45;&gt;B -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>C&#45;&gt;B</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M54.4,-72C62.39,-72 71.31,-72 79.82,-72\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"79.92,-75.5 89.92,-72 79.92,-68.5 79.92,-75.5\"/>\n",
       "</g>\n",
       "<!-- G -->\n",
       "<g id=\"node7\" class=\"node\">\n",
       "<title>G</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"207\" cy=\"-126\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"207\" y=\"-122.3\" font-family=\"Times,serif\" font-size=\"14.00\">G</text>\n",
       "</g>\n",
       "<!-- A&#45;&gt;G -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>A&#45;&gt;G</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M144.4,-126C152.39,-126 161.31,-126 169.82,-126\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"169.92,-129.5 179.92,-126 169.92,-122.5 169.92,-129.5\"/>\n",
       "</g>\n",
       "<!-- E -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>E</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"207\" cy=\"-45\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"207\" y=\"-41.3\" font-family=\"Times,serif\" font-size=\"14.00\">E</text>\n",
       "</g>\n",
       "<!-- B&#45;&gt;E -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>B&#45;&gt;E</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M142.05,-64.62C151.44,-61.74 162.36,-58.39 172.5,-55.28\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"173.75,-58.55 182.29,-52.28 171.7,-51.86 173.75,-58.55\"/>\n",
       "</g>\n",
       "<!-- D -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>D</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-126\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-122.3\" font-family=\"Times,serif\" font-size=\"14.00\">D</text>\n",
       "</g>\n",
       "<!-- D&#45;&gt;A -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>D&#45;&gt;A</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M54.4,-126C62.39,-126 71.31,-126 79.82,-126\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"79.92,-129.5 89.92,-126 79.92,-122.5 79.92,-129.5\"/>\n",
       "</g>\n",
       "<!-- F -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>F</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"117\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"117\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">F</text>\n",
       "</g>\n",
       "<!-- F&#45;&gt;E -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>F&#45;&gt;E</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M142.05,-25.38C151.44,-28.26 162.36,-31.61 172.5,-34.72\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"171.7,-38.14 182.29,-37.72 173.75,-31.45 171.7,-38.14\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f5a9f03d0>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph(graph_attr={\"rankdir\": \"LR\"})\n",
    "g.edge(\"C\", \"A\")\n",
    "g.edge(\"C\", \"B\")\n",
    "g.edge(\"D\", \"A\")\n",
    "g.edge(\"B\", \"E\")\n",
    "g.edge(\"F\", \"E\")\n",
    "g.edge(\"A\", \"G\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:31.066650Z",
     "start_time": "2023-06-25T15:09:30.954496Z"
    }
   },
   "outputs": [],
   "source": [
    "import networkx as nx\n",
    "\n",
    "model = nx.DiGraph([\n",
    "    (\"C\", \"A\"),\n",
    "    (\"C\", \"B\"),\n",
    "    (\"D\", \"A\"),\n",
    "    (\"B\", \"E\"),\n",
    "    (\"F\", \"E\"),\n",
    "    (\"A\", \"G\"),\n",
    "])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:31.073162Z",
     "start_time": "2023-06-25T15:09:31.068365Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Are D and C dependent?\n",
      "False\n",
      "Are D and C dependent given A?\n",
      "True\n",
      "Are D and C dependent given G?\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(\"Are D and C dependent?\")\n",
    "print(not(nx.d_separated(model, {\"D\"}, {\"C\"}, {})))\n",
    "\n",
    "print(\"Are D and C dependent given A?\")\n",
    "print(not(nx.d_separated(model, {\"D\"}, {\"C\"}, {\"A\"})))\n",
    "\n",
    "print(\"Are D and C dependent given G?\")\n",
    "print(not(nx.d_separated(model, {\"D\"}, {\"C\"}, {\"G\"})))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:31.077511Z",
     "start_time": "2023-06-25T15:09:31.074704Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Are G and D dependent?\n",
      "True\n",
      "Are G and D dependent given A?\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print(\"Are G and D dependent?\")\n",
    "print(not(nx.d_separated(model, {\"G\"}, {\"D\"}, {})))\n",
    "\n",
    "print(\"Are G and D dependent given A?\")\n",
    "print(not(nx.d_separated(model, {\"G\"}, {\"D\"}, {\"A\"})))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:31.082324Z",
     "start_time": "2023-06-25T15:09:31.079124Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Are A and B dependent?\n",
      "True\n",
      "Are A and B dependent given C?\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print(\"Are A and B dependent?\")\n",
    "print(not(nx.d_separated(model, {\"A\"}, {\"B\"}, {})))\n",
    "\n",
    "print(\"Are A and B dependent given C?\")\n",
    "print(not(nx.d_separated(model, {\"A\"}, {\"B\"}, {\"C\"})))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:31.087167Z",
     "start_time": "2023-06-25T15:09:31.083938Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Are G and F dependent?\n",
      "False\n",
      "Are G and F dependent given E?\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(\"Are G and F dependent?\")\n",
    "print(not(nx.d_separated(model, {\"G\"}, {\"F\"}, {})))\n",
    "\n",
    "print(\"Are G and F dependent given E?\")\n",
    "print(not(nx.d_separated(model, {\"G\"}, {\"F\"}, {\"E\"})))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    " \n",
    "## Identification Revisited\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:31.290464Z",
     "start_time": "2023-06-25T15:09:31.088696Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"322pt\" height=\"98pt\"\n",
       " viewBox=\"0.00 0.00 322.18 98.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 94)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-94 318.18,-94 318.18,4 -4,4\"/>\n",
       "<!-- profits_prev_6m -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>profits_prev_6m</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"69.54\" cy=\"-45\" rx=\"69.59\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"69.54\" y=\"-41.3\" font-family=\"Times,serif\" font-size=\"14.00\">profits_prev_6m</text>\n",
       "</g>\n",
       "<!-- profits_next_6m -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>profits_next_6m</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"244.63\" cy=\"-72\" rx=\"69.59\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"244.63\" y=\"-68.3\" font-family=\"Times,serif\" font-size=\"14.00\">profits_next_6m</text>\n",
       "</g>\n",
       "<!-- profits_prev_6m&#45;&gt;profits_next_6m -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>profits_prev_6m&#45;&gt;profits_next_6m</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M129.63,-54.22C144.03,-56.46 159.56,-58.89 174.39,-61.2\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"174.1,-64.7 184.52,-62.78 175.18,-57.78 174.1,-64.7\"/>\n",
       "</g>\n",
       "<!-- consultancy -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>consultancy</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"244.63\" cy=\"-18\" rx=\"52.79\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"244.63\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">consultancy</text>\n",
       "</g>\n",
       "<!-- profits_prev_6m&#45;&gt;consultancy -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>profits_prev_6m&#45;&gt;consultancy</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M129.63,-35.78C148.05,-32.91 168.33,-29.75 186.62,-26.89\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"187.2,-30.34 196.54,-25.35 186.12,-23.43 187.2,-30.34\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f4803e390>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "consultancy_sev = gr.Digraph(graph_attr={\"rankdir\": \"LR\"})\n",
    "consultancy_sev.edge(\"profits_prev_6m\", \"profits_next_6m\")\n",
    "consultancy_sev.edge(\"profits_prev_6m\", \"consultancy\")\n",
    "\n",
    "consultancy_sev"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:31.297704Z",
     "start_time": "2023-06-25T15:09:31.292738Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "consultancy_model_severed = nx.DiGraph([\n",
    "    (\"profits_prev_6m\", \"profits_next_6m\"),\n",
    "    (\"profits_prev_6m\", \"consultancy\"),\n",
    "#     (\"consultancy\", \"profits_next_6m\"), # causal relationship removed\n",
    "])\n",
    "\n",
    "not(nx.d_separated(consultancy_model_severed,\n",
    "                   {\"consultancy\"}, {\"profits_next_6m\"}, {}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:31.505182Z",
     "start_time": "2023-06-25T15:09:31.299411Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"463pt\" height=\"67pt\"\n",
       " viewBox=\"0.00 0.00 463.47 67.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 63)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-63 459.47,-63 459.47,4 -4,4\"/>\n",
       "<!-- profits_prev_6m -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>profits_prev_6m</title>\n",
       "<ellipse fill=\"lightgrey\" stroke=\"lightgrey\" cx=\"69.54\" cy=\"-41\" rx=\"69.59\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"69.54\" y=\"-37.3\" font-family=\"Times,serif\" font-size=\"14.00\">profits_prev_6m</text>\n",
       "</g>\n",
       "<!-- profits_next_6m -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>profits_next_6m</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"385.92\" cy=\"-41\" rx=\"69.59\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"385.92\" y=\"-37.3\" font-family=\"Times,serif\" font-size=\"14.00\">profits_next_6m</text>\n",
       "</g>\n",
       "<!-- profits_prev_6m&#45;&gt;profits_next_6m -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>profits_prev_6m&#45;&gt;profits_next_6m</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M138.3,-43.97C150.54,-44.4 163.19,-44.77 175.09,-45 221.87,-45.89 233.59,-45.89 280.38,-45 289.02,-44.83 298.06,-44.59 307.06,-44.31\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"307.29,-47.8 317.17,-43.97 307.06,-40.81 307.29,-47.8\"/>\n",
       "</g>\n",
       "<!-- consultancy -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>consultancy</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"227.73\" cy=\"-18\" rx=\"52.79\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"227.73\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">consultancy</text>\n",
       "</g>\n",
       "<!-- profits_prev_6m&#45;&gt;consultancy -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>profits_prev_6m&#45;&gt;consultancy</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M130.48,-32.17C142.98,-30.33 156.14,-28.39 168.58,-26.56\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"169.44,-29.97 178.82,-25.06 168.42,-23.05 169.44,-29.97\"/>\n",
       "</g>\n",
       "<!-- consultancy&#45;&gt;profits_next_6m -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>consultancy&#45;&gt;profits_next_6m</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M276.44,-25.03C288.67,-26.83 302.07,-28.8 315.16,-30.73\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"314.67,-34.19 325.08,-32.19 315.69,-27.27 314.67,-34.19\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f48042990>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g_consultancy = gr.Digraph(graph_attr={\"rankdir\":\"LR\"})\n",
    "g_consultancy.edge(\"profits_prev_6m\", \"profits_next_6m\")\n",
    "g_consultancy.edge(\"profits_prev_6m\", \"consultancy\")\n",
    "g_consultancy.edge(\"consultancy\", \"profits_next_6m\")\n",
    "g_consultancy.node(\"profits_prev_6m\", color=\"lightgrey\", style=\"filled\")\n",
    "\n",
    "g_consultancy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## CIA and The Adjustment Formula\n",
    " \n",
    "## Positivity Assumption\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## An Identification Example with Data\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:31.518818Z",
     "start_time": "2023-06-25T15:09:31.507110Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>profits_prev_6m</th>\n",
       "      <th>consultancy</th>\n",
       "      <th>profits_next_6m</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5.0</td>\n",
       "      <td>0</td>\n",
       "      <td>5.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5.7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   profits_prev_6m  consultancy  profits_next_6m\n",
       "0              1.0            0              1.0\n",
       "1              1.0            0              1.1\n",
       "2              1.0            1              1.2\n",
       "3              5.0            0              5.5\n",
       "4              5.0            1              5.7\n",
       "5              5.0            1              5.7"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(dict(\n",
    "    profits_prev_6m=[1.0, 1.0, 1.0, 5.0, 5.0, 5.0],\n",
    "    consultancy=[0, 0, 1, 0, 1, 1],\n",
    "    profits_next_6m=[1, 1.1, 1.2, 5.5, 5.7, 5.7],\n",
    "))\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:31.528085Z",
     "start_time": "2023-06-25T15:09:31.520733Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.666666666666667"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(df.query(\"consultancy==1\")[\"profits_next_6m\"].mean() \n",
    " - df.query(\"consultancy==0\")[\"profits_next_6m\"].mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:31.538760Z",
     "start_time": "2023-06-25T15:09:31.529677Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "profits_prev_6m\n",
       "1.0    0.15\n",
       "5.0    0.20\n",
       "Name: profits_next_6m, dtype: float64"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "avg_df = (df\n",
    "          .groupby([\"consultancy\", \"profits_prev_6m\"])\n",
    "          [\"profits_next_6m\"]\n",
    "          .mean())\n",
    "\n",
    "avg_df.loc[1] - avg_df.loc[0] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:31.745712Z",
     "start_time": "2023-06-25T15:09:31.540150Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"332pt\" height=\"67pt\"\n",
       " viewBox=\"0.00 0.00 332.00 67.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 63)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-63 328,-63 328,4 -4,4\"/>\n",
       "<!-- U -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>U</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">U</text>\n",
       "</g>\n",
       "<!-- T -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>T</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"117\" cy=\"-41\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"117\" y=\"-37.3\" font-family=\"Times,serif\" font-size=\"14.00\">T</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;T -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>U&#45;&gt;T</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M52.51,-24.41C61.57,-26.77 72.01,-29.5 81.78,-32.05\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"81.06,-35.49 91.62,-34.63 82.83,-28.71 81.06,-35.49\"/>\n",
       "</g>\n",
       "<!-- Y -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"297\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"297\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;Y -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>U&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M53.92,-15.95C65.03,-15.17 78.14,-14.38 90,-14 153.97,-11.94 170.03,-11.94 234,-14 242.43,-14.27 251.5,-14.75 260.02,-15.28\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"259.87,-18.78 270.08,-15.95 260.33,-11.8 259.87,-18.78\"/>\n",
       "</g>\n",
       "<!-- M -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>M</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"207\" cy=\"-41\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"207\" y=\"-37.3\" font-family=\"Times,serif\" font-size=\"14.00\">M</text>\n",
       "</g>\n",
       "<!-- T&#45;&gt;M -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>T&#45;&gt;M</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M144.4,-41C152.39,-41 161.31,-41 169.82,-41\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"169.92,-44.5 179.92,-41 169.92,-37.5 169.92,-44.5\"/>\n",
       "</g>\n",
       "<!-- M&#45;&gt;Y -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>M&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M232.51,-34.59C241.57,-32.23 252.01,-29.5 261.78,-26.95\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"262.83,-30.29 271.62,-24.37 261.06,-23.51 262.83,-30.29\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f79cb3bd0>"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph(graph_attr={\"rankdir\":\"LR\"})\n",
    "g.edge(\"U\", \"T\")\n",
    "g.edge(\"U\", \"Y\")\n",
    "g.edge(\"T\", \"M\")\n",
    "g.edge(\"M\", \"Y\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Confounding Bias\n",
    " \n",
    "The first significant cause of bias is confounding. It's the bias we've been discussing so far. Now, we are just putting a name to it. **Confounding happens when there is an open backdoor path through which association flows non-causally, usually because the treatment and the outcome share a common cause**. For example, let's say that you work in HR and you want to know if your new management training program is increasing employers' engagement. However, since the training is optional, you believe only managers that are already doing great attend the program and those who need it the most, don't. When you measure engagement of the teams under the managers that took the training, it is much higher than that of the teams under the managers who didn't attend the training. But it's hard to know how much of this is causal. Since there is a common cause between treatment and outcome (the training and being a good manager, respectively) they would move together regardless of a causal effect. \n",
    " \n",
    "To identify that causal effect, you need to close all backdoor paths between the treatment and the outcome. If you do so, the only effect that will be left is the direct effect $T \\to Y$. In our example, you could somehow control for the manager's quality prior to taking the training. In that situation, the difference in the outcome will be only due to the training, since manager quality prior to the training would be held constant between treatment and control. Simply put, **to adjust for confounding bias, you need to adjust for the common causes of the treatment and the outcome**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:31.958504Z",
     "start_time": "2023-06-25T15:09:31.756409Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"415pt\" height=\"166pt\"\n",
       " viewBox=\"0.00 0.00 415.37 166.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 162)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-162 411.37,-162 411.37,4 -4,4\"/>\n",
       "<!-- X -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>X</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"71.49\" cy=\"-34\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"71.49\" y=\"-30.3\" font-family=\"Times,serif\" font-size=\"14.00\">X</text>\n",
       "</g>\n",
       "<!-- T -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>T</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"219.93\" cy=\"-41\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"219.93\" y=\"-37.3\" font-family=\"Times,serif\" font-size=\"14.00\">T</text>\n",
       "</g>\n",
       "<!-- X&#45;&gt;T -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>X&#45;&gt;T</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M98.67,-35.25C122.1,-36.37 156.67,-38.02 182.74,-39.27\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"182.82,-42.78 192.98,-39.76 183.16,-35.79 182.82,-42.78\"/>\n",
       "</g>\n",
       "<!-- Y -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"352.13\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"352.13\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- X&#45;&gt;Y -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>X&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M97.27,-28C118.74,-23.12 150.7,-16.63 178.99,-14 215.23,-10.63 224.49,-13.19 260.88,-14 278.63,-14.39 298.33,-15.22 314.78,-16.02\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"314.91,-19.53 325.07,-16.54 315.26,-12.54 314.91,-19.53\"/>\n",
       "</g>\n",
       "<!-- T&#45;&gt;Y -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>T&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M246.34,-36.51C266.15,-33.01 293.88,-28.11 315.83,-24.23\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"316.54,-27.66 325.78,-22.48 315.32,-20.77 316.54,-27.66\"/>\n",
       "</g>\n",
       "<!-- Manager Quality -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>Manager Quality</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"71.49\" cy=\"-107\" rx=\"71.49\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"71.49\" y=\"-103.3\" font-family=\"Times,serif\" font-size=\"14.00\">Manager Quality</text>\n",
       "</g>\n",
       "<!-- Training -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>Training</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"219.93\" cy=\"-140\" rx=\"40.89\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"219.93\" y=\"-136.3\" font-family=\"Times,serif\" font-size=\"14.00\">Training</text>\n",
       "</g>\n",
       "<!-- Manager Quality&#45;&gt;Training -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>Manager Quality&#45;&gt;Training</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M125.36,-118.91C140.94,-122.42 157.85,-126.23 172.96,-129.64\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"172.56,-133.14 183.09,-131.92 174.1,-126.31 172.56,-133.14\"/>\n",
       "</g>\n",
       "<!-- Engagement -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>Engagement</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"352.13\" cy=\"-117\" rx=\"55.49\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"352.13\" y=\"-113.3\" font-family=\"Times,serif\" font-size=\"14.00\">Engagement</text>\n",
       "</g>\n",
       "<!-- Manager Quality&#45;&gt;Engagement -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>Manager Quality&#45;&gt;Engagement</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M142.73,-109.03C178.16,-110.11 221.8,-111.51 260.88,-113 269.33,-113.32 278.22,-113.69 286.99,-114.06\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"287.06,-117.57 297.21,-114.5 287.37,-110.57 287.06,-117.57\"/>\n",
       "</g>\n",
       "<!-- Training&#45;&gt;Engagement -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>Training&#45;&gt;Engagement</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M258.24,-133.41C269.16,-131.48 281.36,-129.32 293.24,-127.23\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"293.87,-130.67 303.11,-125.48 292.65,-123.78 293.87,-130.67\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f48049790>"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph(graph_attr={\"rankdir\":\"LR\"})\n",
    "g.edge(\"X\", \"T\")\n",
    "g.edge(\"X\", \"Y\")\n",
    "g.edge(\"T\", \"Y\")\n",
    "\n",
    "g.edge(\"Manager Quality\", \"Training\"),\n",
    "g.edge(\"Manager Quality\", \"Engagement\"),\n",
    "g.edge(\"Training\", \"Engagement\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "### Surrogate Confounding\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:32.157902Z",
     "start_time": "2023-06-25T15:09:31.960159Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"733pt\" height=\"260pt\"\n",
       " viewBox=\"0.00 0.00 732.89 260.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 256)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-256 728.89,-256 728.89,4 -4,4\"/>\n",
       "<!-- X1 -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>X1</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"99\" cy=\"-234\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"99\" y=\"-230.3\" font-family=\"Times,serif\" font-size=\"14.00\">X1</text>\n",
       "</g>\n",
       "<!-- U -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>U</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"99\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"99\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">U</text>\n",
       "</g>\n",
       "<!-- X1&#45;&gt;U -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>X1&#45;&gt;U</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M99,-215.7C99,-207.98 99,-198.71 99,-190.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"102.5,-190.1 99,-180.1 95.5,-190.1 102.5,-190.1\"/>\n",
       "</g>\n",
       "<!-- X2 -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>X2</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">X2</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;X2 -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>U&#45;&gt;X2</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M84.43,-146.83C74.25,-136.94 60.48,-123.55 48.97,-112.36\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"51.41,-109.85 41.8,-105.38 46.53,-114.87 51.41,-109.85\"/>\n",
       "</g>\n",
       "<!-- T -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>T</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"99\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"99\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">T</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;T -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>U&#45;&gt;T</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M99,-143.7C99,-135.98 99,-126.71 99,-118.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"102.5,-118.1 99,-108.1 95.5,-118.1 102.5,-118.1\"/>\n",
       "</g>\n",
       "<!-- Y -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"127\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"127\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;Y -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>U&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M112.75,-146.07C120.96,-136.1 130.62,-122.25 135,-108 141.25,-87.67 138.51,-63.59 134.62,-45.61\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"138,-44.7 132.25,-35.8 131.2,-46.34 138,-44.7\"/>\n",
       "</g>\n",
       "<!-- T&#45;&gt;Y -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>T&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M105.64,-72.41C108.91,-64.22 112.94,-54.14 116.62,-44.95\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"119.95,-46.05 120.41,-35.47 113.45,-43.45 119.95,-46.05\"/>\n",
       "</g>\n",
       "<!-- Manager Quality -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>Manager Quality</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"517\" cy=\"-162\" rx=\"71.49\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"517\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">Manager Quality</text>\n",
       "</g>\n",
       "<!-- Team&#39;s Attrition -->\n",
       "<g id=\"node7\" class=\"node\">\n",
       "<title>Team&#39;s Attrition</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"250\" cy=\"-90\" rx=\"68.49\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"250\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">Team&#39;s Attrition</text>\n",
       "</g>\n",
       "<!-- Manager Quality&#45;&gt;Team&#39;s Attrition -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>Manager Quality&#45;&gt;Team&#39;s Attrition</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M469.27,-148.49C423.66,-136.53 354.89,-118.5 306.56,-105.83\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"307.43,-102.44 296.87,-103.29 305.66,-109.21 307.43,-102.44\"/>\n",
       "</g>\n",
       "<!-- Team&#39;s Past Performance -->\n",
       "<g id=\"node8\" class=\"node\">\n",
       "<title>Team&#39;s Past Performance</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"437\" cy=\"-90\" rx=\"100.98\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"437\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">Team&#39;s Past Performance</text>\n",
       "</g>\n",
       "<!-- Manager Quality&#45;&gt;Team&#39;s Past Performance -->\n",
       "<g id=\"edge7\" class=\"edge\">\n",
       "<title>Manager Quality&#45;&gt;Team&#39;s Past Performance</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M498.04,-144.41C487.85,-135.5 475.12,-124.36 463.92,-114.56\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"465.93,-111.66 456.1,-107.71 461.32,-116.93 465.93,-111.66\"/>\n",
       "</g>\n",
       "<!-- Training -->\n",
       "<g id=\"node11\" class=\"node\">\n",
       "<title>Training</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"597\" cy=\"-90\" rx=\"40.89\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"597\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">Training</text>\n",
       "</g>\n",
       "<!-- Manager Quality&#45;&gt;Training -->\n",
       "<g id=\"edge10\" class=\"edge\">\n",
       "<title>Manager Quality&#45;&gt;Training</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M535.96,-144.41C546.62,-135.08 560.07,-123.31 571.63,-113.2\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"574.05,-115.73 579.27,-106.51 569.44,-110.46 574.05,-115.73\"/>\n",
       "</g>\n",
       "<!-- Engagement -->\n",
       "<g id=\"node12\" class=\"node\">\n",
       "<title>Engagement</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"631\" cy=\"-18\" rx=\"55.49\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"631\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Engagement</text>\n",
       "</g>\n",
       "<!-- Manager Quality&#45;&gt;Engagement -->\n",
       "<g id=\"edge12\" class=\"edge\">\n",
       "<title>Manager Quality&#45;&gt;Engagement</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M576.62,-151.93C602.81,-144.66 631.16,-131.57 647,-108 659.46,-89.47 653.13,-64.06 645.06,-45.14\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"648.15,-43.49 640.76,-35.9 641.8,-46.45 648.15,-43.49\"/>\n",
       "</g>\n",
       "<!-- Manager&#39;s Tenure -->\n",
       "<g id=\"node9\" class=\"node\">\n",
       "<title>Manager&#39;s Tenure</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"417\" cy=\"-234\" rx=\"74.19\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"417\" y=\"-230.3\" font-family=\"Times,serif\" font-size=\"14.00\">Manager&#39;s Tenure</text>\n",
       "</g>\n",
       "<!-- Manager&#39;s Tenure&#45;&gt;Manager Quality -->\n",
       "<g id=\"edge8\" class=\"edge\">\n",
       "<title>Manager&#39;s Tenure&#45;&gt;Manager Quality</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M440.19,-216.76C453.7,-207.31 470.91,-195.26 485.61,-184.97\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"487.75,-187.75 493.93,-179.15 483.73,-182.01 487.75,-187.75\"/>\n",
       "</g>\n",
       "<!-- Manager&#39;s Education Level -->\n",
       "<g id=\"node10\" class=\"node\">\n",
       "<title>Manager&#39;s Education Level</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"617\" cy=\"-234\" rx=\"107.78\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"617\" y=\"-230.3\" font-family=\"Times,serif\" font-size=\"14.00\">Manager&#39;s Education Level</text>\n",
       "</g>\n",
       "<!-- Manager&#39;s Education Level&#45;&gt;Manager Quality -->\n",
       "<g id=\"edge9\" class=\"edge\">\n",
       "<title>Manager&#39;s Education Level&#45;&gt;Manager Quality</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M593.3,-216.41C579.88,-207.02 562.92,-195.15 548.4,-184.98\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"550.38,-182.1 540.18,-179.23 546.37,-187.83 550.38,-182.1\"/>\n",
       "</g>\n",
       "<!-- Training&#45;&gt;Engagement -->\n",
       "<g id=\"edge11\" class=\"edge\">\n",
       "<title>Training&#45;&gt;Engagement</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M605.23,-72.05C609.1,-64.09 613.8,-54.41 618.12,-45.51\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"621.39,-46.8 622.61,-36.28 615.09,-43.74 621.39,-46.8\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f79ceda10>"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph()\n",
    "g.edge(\"X1\", \"U\")\n",
    "g.edge(\"U\", \"X2\")\n",
    "g.edge(\"U\", \"T\")\n",
    "g.edge(\"T\", \"Y\")\n",
    "g.edge(\"U\", \"Y\")\n",
    "\n",
    "g.edge(\"Manager Quality\", \"Team's Attrition\")\n",
    "g.edge(\"Manager Quality\", \"Team's Past Performance\")\n",
    "g.edge(\"Manager's Tenure\", \"Manager Quality\")\n",
    "g.edge(\"Manager's Education Level\", \"Manager Quality\")\n",
    "\n",
    "g.edge(\"Manager Quality\", \"Training\")\n",
    "g.edge(\"Training\", \"Engagement\")\n",
    "g.edge(\"Manager Quality\", \"Engagement\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Randomization Revisited\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:32.361584Z",
     "start_time": "2023-06-25T15:09:32.160291Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"242pt\" height=\"98pt\"\n",
       " viewBox=\"0.00 0.00 242.00 98.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 94)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-94 238,-94 238,4 -4,4\"/>\n",
       "<!-- rnd -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>rnd</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-72\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-68.3\" font-family=\"Times,serif\" font-size=\"14.00\">rnd</text>\n",
       "</g>\n",
       "<!-- T -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>T</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"117\" cy=\"-72\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"117\" y=\"-68.3\" font-family=\"Times,serif\" font-size=\"14.00\">T</text>\n",
       "</g>\n",
       "<!-- rnd&#45;&gt;T -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>rnd&#45;&gt;T</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M54.4,-72C62.39,-72 71.31,-72 79.82,-72\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"79.92,-75.5 89.92,-72 79.92,-68.5 79.92,-75.5\"/>\n",
       "</g>\n",
       "<!-- Y -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"207\" cy=\"-45\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"207\" y=\"-41.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- T&#45;&gt;Y -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>T&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M142.05,-64.62C151.44,-61.74 162.36,-58.39 172.5,-55.28\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"173.75,-58.55 182.29,-52.28 171.7,-51.86 173.75,-58.55\"/>\n",
       "</g>\n",
       "<!-- U -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>U</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"117\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"117\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">U</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;Y -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>U&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M142.05,-25.38C151.44,-28.26 162.36,-31.61 172.5,-34.72\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"171.7,-38.14 182.29,-37.72 173.75,-31.45 171.7,-38.14\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f79ced310>"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph(graph_attr={\"rankdir\":\"LR\"})\n",
    "g.edge(\"rnd\", \"T\")\n",
    "g.edge(\"T\", \"Y\")\n",
    "g.edge(\"U\", \"Y\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Selection Bias\n",
    " \n",
    "### Conditioning on a Collider\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:32.574282Z",
     "start_time": "2023-06-25T15:09:32.363619Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"553pt\" height=\"180pt\"\n",
       " viewBox=\"0.00 0.00 552.76 180.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 176)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-176 548.76,-176 548.76,4 -4,4\"/>\n",
       "<!-- T -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>T</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"29.25\" cy=\"-50\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"29.25\" y=\"-46.3\" font-family=\"Times,serif\" font-size=\"14.00\">T</text>\n",
       "</g>\n",
       "<!-- S -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>S</title>\n",
       "<ellipse fill=\"lightgrey\" stroke=\"lightgrey\" cx=\"331.33\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"331.33\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">S</text>\n",
       "</g>\n",
       "<!-- T&#45;&gt;S -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>T&#45;&gt;S</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M56.31,-50.77C90.77,-51.44 153.32,-51.46 206.28,-45 236.91,-41.26 271.14,-33.54 295.87,-27.31\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"297.03,-30.62 305.85,-24.74 295.29,-23.84 297.03,-30.62\"/>\n",
       "</g>\n",
       "<!-- Y -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"150.39\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"150.39\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- T&#45;&gt;Y -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>T&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M54.65,-43.45C72.15,-38.74 95.98,-32.35 115.35,-27.14\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"116.27,-30.52 125.02,-24.54 114.46,-23.76 116.27,-30.52\"/>\n",
       "</g>\n",
       "<!-- Y&#45;&gt;S -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>Y&#45;&gt;S</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M177.41,-18C208.15,-18 259.49,-18 294.11,-18\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"294.28,-21.5 304.28,-18 294.28,-14.5 294.28,-21.5\"/>\n",
       "</g>\n",
       "<!-- RND -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>RND</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"29.25\" cy=\"-123\" rx=\"29.5\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"29.25\" y=\"-119.3\" font-family=\"Times,serif\" font-size=\"14.00\">RND</text>\n",
       "</g>\n",
       "<!-- New Feature -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>New Feature</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"150.39\" cy=\"-123\" rx=\"55.79\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"150.39\" y=\"-119.3\" font-family=\"Times,serif\" font-size=\"14.00\">New Feature</text>\n",
       "</g>\n",
       "<!-- RND&#45;&gt;New Feature -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>RND&#45;&gt;New Feature</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M58.78,-123C66.59,-123 75.38,-123 84.33,-123\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"84.47,-126.5 94.47,-123 84.47,-119.5 84.47,-126.5\"/>\n",
       "</g>\n",
       "<!-- Customer Satisfaction -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>Customer Satisfaction</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"331.33\" cy=\"-146\" rx=\"89.08\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"331.33\" y=\"-142.3\" font-family=\"Times,serif\" font-size=\"14.00\">Customer Satisfaction</text>\n",
       "</g>\n",
       "<!-- New Feature&#45;&gt;Customer Satisfaction -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>New Feature&#45;&gt;Customer Satisfaction</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M202.67,-129.59C216.09,-131.32 230.93,-133.22 245.6,-135.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"245.34,-138.6 255.7,-136.41 246.23,-131.66 245.34,-138.6\"/>\n",
       "</g>\n",
       "<!-- Response -->\n",
       "<g id=\"node8\" class=\"node\">\n",
       "<title>Response</title>\n",
       "<ellipse fill=\"lightgrey\" stroke=\"lightgrey\" cx=\"500.57\" cy=\"-100\" rx=\"44.39\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"500.57\" y=\"-96.3\" font-family=\"Times,serif\" font-size=\"14.00\">Response</text>\n",
       "</g>\n",
       "<!-- New Feature&#45;&gt;Response -->\n",
       "<g id=\"edge8\" class=\"edge\">\n",
       "<title>New Feature&#45;&gt;Response</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M205.41,-119.43C271.13,-115.09 381.38,-107.81 446.61,-103.5\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"446.94,-106.98 456.68,-102.83 446.47,-100 446.94,-106.98\"/>\n",
       "</g>\n",
       "<!-- NPS -->\n",
       "<g id=\"node7\" class=\"node\">\n",
       "<title>NPS</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"500.57\" cy=\"-154\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"500.57\" y=\"-150.3\" font-family=\"Times,serif\" font-size=\"14.00\">NPS</text>\n",
       "</g>\n",
       "<!-- Customer Satisfaction&#45;&gt;NPS -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>Customer Satisfaction&#45;&gt;NPS</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M418.4,-150.12C434.18,-150.87 449.85,-151.62 463.16,-152.26\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"463.12,-155.76 473.27,-152.74 463.45,-148.77 463.12,-155.76\"/>\n",
       "</g>\n",
       "<!-- Customer Satisfaction&#45;&gt;Response -->\n",
       "<g id=\"edge7\" class=\"edge\">\n",
       "<title>Customer Satisfaction&#45;&gt;Response</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M384.78,-131.57C406.86,-125.5 432.33,-118.49 453.65,-112.63\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"454.73,-115.96 463.44,-109.94 452.87,-109.21 454.73,-115.96\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f79cf5110>"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph(graph_attr={\"rankdir\": \"LR\"})\n",
    "g.edge(\"T\", \"S\")\n",
    "g.edge(\"T\", \"Y\")\n",
    "g.edge(\"Y\", \"S\")\n",
    "g.node(\"S\", color=\"lightgrey\", style=\"filled\")\n",
    "\n",
    "g.edge(\"RND\", \"New Feature\"),\n",
    "g.edge(\"New Feature\", \"Customer Satisfaction\"),\n",
    "g.edge(\"Customer Satisfaction\", \"NPS\"),\n",
    "g.edge(\"Customer Satisfaction\", \"Response\"),\n",
    "g.edge(\"New Feature\", \"Response\"),\n",
    "g.node(\"Response\", \"Response\", color=\"lightgrey\", style=\"filled\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:32.581278Z",
     "start_time": "2023-06-25T15:09:32.576515Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nps_model = nx.DiGraph([\n",
    "   (\"RND\", \"New Feature\"),\n",
    "#     (\"New Feature\", \"Customer Satisfaction\"),\n",
    "    (\"Customer Satisfaction\", \"NPS\"),\n",
    "    (\"Customer Satisfaction\", \"Response\"),\n",
    "    (\"New Feature\", \"Response\"),\n",
    "])\n",
    "\n",
    "\n",
    "not(nx.d_separated(nps_model, {\"NPS\"}, {\"New Feature\"}, {\"Response\"}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:32.622363Z",
     "start_time": "2023-06-25T15:09:32.582667Z"
    },
    "scrolled": true,
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>responded</th>\n",
       "      <th>nps_0</th>\n",
       "      <th>nps_1</th>\n",
       "      <th>nps</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>new_feature</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.183715</td>\n",
       "      <td>-0.005047</td>\n",
       "      <td>0.395015</td>\n",
       "      <td>-0.005047</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.639342</td>\n",
       "      <td>-0.005239</td>\n",
       "      <td>0.401082</td>\n",
       "      <td>0.401082</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             responded     nps_0     nps_1       nps\n",
       "new_feature                                         \n",
       "0             0.183715 -0.005047  0.395015 -0.005047\n",
       "1             0.639342 -0.005239  0.401082  0.401082"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.seed(2)\n",
    "n = 100000\n",
    "new_feature = np.random.binomial(1, 0.5, n)\n",
    "\n",
    "satisfaction_0 = np.random.normal(0, 0.5, n)\n",
    "satisfaction_1 = satisfaction_0 + 0.4\n",
    "satisfaction = new_feature*satisfaction_1 + (1-new_feature)*satisfaction_0\n",
    "\n",
    "nps_0 = np.random.normal(satisfaction_0, 1)\n",
    "nps_1 = np.random.normal(satisfaction_1, 1)\n",
    "nps = new_feature*nps_1 + (1-new_feature)*nps_0\n",
    "\n",
    "\n",
    "responded  = (np.random.normal(0 + new_feature + satisfaction, 1) > 1).astype(int)\n",
    "\n",
    "tr_df = pd.DataFrame(dict(new_feature=new_feature,\n",
    "                          responded=responded,\n",
    "                          nps_0=nps_0,\n",
    "                          nps_1=nps_1,\n",
    "                          nps=nps))\n",
    "\n",
    "tr_df_measurable = pd.DataFrame(dict(new_feature=new_feature,\n",
    "                                     responded=responded,\n",
    "                                     nps_0=np.nan,\n",
    "                                     nps_1=np.nan,\n",
    "                                     nps=np.where(responded, nps, np.nan)))\n",
    "\n",
    "tr_df.groupby(\"new_feature\").mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:32.634960Z",
     "start_time": "2023-06-25T15:09:32.624532Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>responded</th>\n",
       "      <th>nps_0</th>\n",
       "      <th>nps_1</th>\n",
       "      <th>nps</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>new_feature</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.183715</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.639342</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             responded  nps_0  nps_1  nps\n",
       "new_feature                              \n",
       "0             0.183715    NaN    NaN  NaN\n",
       "1             0.639342    NaN    NaN  NaN"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tr_df_measurable.groupby(\"new_feature\").mean().assign(**{\"nps\": np.nan})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:32.647398Z",
     "start_time": "2023-06-25T15:09:32.636260Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>nps_0</th>\n",
       "      <th>nps_1</th>\n",
       "      <th>nps</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>responded</th>\n",
       "      <th>new_feature</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">0</th>\n",
       "      <th>0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1</th>\n",
       "      <th>0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.314073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.536106</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                       nps_0  nps_1       nps\n",
       "responded new_feature                        \n",
       "0         0              NaN    NaN       NaN\n",
       "          1              NaN    NaN       NaN\n",
       "1         0              NaN    NaN  0.314073\n",
       "          1              NaN    NaN  0.536106"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tr_df_measurable.groupby([\"responded\", \"new_feature\"]).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:32.658749Z",
     "start_time": "2023-06-25T15:09:32.648693Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>nps_0</th>\n",
       "      <th>nps_1</th>\n",
       "      <th>nps</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>responded</th>\n",
       "      <th>new_feature</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">0</th>\n",
       "      <th>0</th>\n",
       "      <td>-0.076869</td>\n",
       "      <td>0.320616</td>\n",
       "      <td>-0.076869</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.234852</td>\n",
       "      <td>0.161725</td>\n",
       "      <td>0.161725</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1</th>\n",
       "      <th>0</th>\n",
       "      <td>0.314073</td>\n",
       "      <td>0.725585</td>\n",
       "      <td>0.314073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.124287</td>\n",
       "      <td>0.536106</td>\n",
       "      <td>0.536106</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                          nps_0     nps_1       nps\n",
       "responded new_feature                              \n",
       "0         0           -0.076869  0.320616 -0.076869\n",
       "          1           -0.234852  0.161725  0.161725\n",
       "1         0            0.314073  0.725585  0.314073\n",
       "          1            0.124287  0.536106  0.536106"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tr_df.groupby([\"responded\", \"new_feature\"]).mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Adjusting for Selection Bias\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:32.862849Z",
     "start_time": "2023-06-25T15:09:32.660473Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"561pt\" height=\"188pt\"\n",
       " viewBox=\"0.00 0.00 560.90 188.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 184)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-184 556.9,-184 556.9,4 -4,4\"/>\n",
       "<!-- U -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>U</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"54\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"54\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">U</text>\n",
       "</g>\n",
       "<!-- X -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>X</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">X</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;X -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>U&#45;&gt;X</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M47.6,-144.41C44.49,-136.34 40.67,-126.43 37.17,-117.35\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"40.4,-116.03 33.54,-107.96 33.87,-118.55 40.4,-116.03\"/>\n",
       "</g>\n",
       "<!-- Y -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"99\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"99\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;Y -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>U&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M64.21,-145.12C69.85,-136.34 76.97,-125.26 83.3,-115.42\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"86.37,-117.13 88.83,-106.82 80.48,-113.34 86.37,-117.13\"/>\n",
       "</g>\n",
       "<!-- S -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>S</title>\n",
       "<ellipse fill=\"lightgrey\" stroke=\"lightgrey\" cx=\"116\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"116\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">S</text>\n",
       "</g>\n",
       "<!-- X&#45;&gt;S -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>X&#45;&gt;S</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M43.74,-75.83C57.08,-65.34 75.96,-50.49 91.08,-38.6\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"93.34,-41.28 99.03,-32.35 89.01,-35.78 93.34,-41.28\"/>\n",
       "</g>\n",
       "<!-- T -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>T</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"140\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"140\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">T</text>\n",
       "</g>\n",
       "<!-- T&#45;&gt;S -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>T&#45;&gt;S</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M140.49,-143.75C140.66,-125.68 139.98,-96.57 135,-72 133.16,-62.91 130.07,-53.28 126.91,-44.76\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"130.14,-43.4 123.25,-35.36 123.62,-45.95 130.14,-43.4\"/>\n",
       "</g>\n",
       "<!-- T&#45;&gt;Y -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>T&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M130.49,-144.76C125.47,-136.19 119.2,-125.49 113.58,-115.9\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"116.53,-114.01 108.46,-107.15 110.49,-117.55 116.53,-114.01\"/>\n",
       "</g>\n",
       "<!-- New Feature -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>New Feature</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"275\" cy=\"-162\" rx=\"55.79\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"275\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">New Feature</text>\n",
       "</g>\n",
       "<!-- Customer Satisfaction -->\n",
       "<g id=\"node7\" class=\"node\">\n",
       "<title>Customer Satisfaction</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"334\" cy=\"-90\" rx=\"89.08\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"334\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">Customer Satisfaction</text>\n",
       "</g>\n",
       "<!-- New Feature&#45;&gt;Customer Satisfaction -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>New Feature&#45;&gt;Customer Satisfaction</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M288.98,-144.41C296.22,-135.82 305.21,-125.16 313.24,-115.63\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"315.94,-117.86 319.71,-107.96 310.59,-113.35 315.94,-117.86\"/>\n",
       "</g>\n",
       "<!-- Response -->\n",
       "<g id=\"node10\" class=\"node\">\n",
       "<title>Response</title>\n",
       "<ellipse fill=\"lightgrey\" stroke=\"lightgrey\" cx=\"255\" cy=\"-18\" rx=\"44.39\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"255\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Response</text>\n",
       "</g>\n",
       "<!-- New Feature&#45;&gt;Response -->\n",
       "<g id=\"edge10\" class=\"edge\">\n",
       "<title>New Feature&#45;&gt;Response</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M258.52,-144.46C250.01,-134.73 240.49,-121.68 236,-108 229.18,-87.23 235.28,-62.91 242.33,-44.95\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"245.58,-46.26 246.27,-35.68 239.14,-43.52 245.58,-46.26\"/>\n",
       "</g>\n",
       "<!-- Unknown Stuff -->\n",
       "<g id=\"node8\" class=\"node\">\n",
       "<title>Unknown Stuff</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"443\" cy=\"-162\" rx=\"65.79\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"443\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">Unknown Stuff</text>\n",
       "</g>\n",
       "<!-- Unknown Stuff&#45;&gt;Customer Satisfaction -->\n",
       "<g id=\"edge7\" class=\"edge\">\n",
       "<title>Unknown Stuff&#45;&gt;Customer Satisfaction</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M418.54,-145.29C403.71,-135.77 384.57,-123.47 368.25,-112.99\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"369.82,-109.85 359.52,-107.39 366.04,-115.74 369.82,-109.85\"/>\n",
       "</g>\n",
       "<!-- Time in App -->\n",
       "<g id=\"node9\" class=\"node\">\n",
       "<title>Time in App</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"497\" cy=\"-90\" rx=\"55.79\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"497\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">Time in App</text>\n",
       "</g>\n",
       "<!-- Unknown Stuff&#45;&gt;Time in App -->\n",
       "<g id=\"edge8\" class=\"edge\">\n",
       "<title>Unknown Stuff&#45;&gt;Time in App</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M456.07,-144.05C462.57,-135.63 470.56,-125.28 477.74,-115.97\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"480.71,-117.84 484.05,-107.79 475.17,-113.57 480.71,-117.84\"/>\n",
       "</g>\n",
       "<!-- Time in App&#45;&gt;Response -->\n",
       "<g id=\"edge9\" class=\"edge\">\n",
       "<title>Time in App&#45;&gt;Response</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M456.77,-77.36C413.53,-64.86 344.98,-45.03 299.99,-32.01\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"300.79,-28.6 290.21,-29.18 298.84,-35.33 300.79,-28.6\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f5ae674d0>"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph()\n",
    "\n",
    "g.edge(\"U\", \"X\")\n",
    "g.edge(\"X\", \"S\")\n",
    "g.edge(\"U\", \"Y\")\n",
    "g.edge(\"T\", \"Y\")\n",
    "g.edge(\"T\", \"S\")\n",
    "g.node(\"S\", color=\"lightgrey\", style=\"filled\")\n",
    "\n",
    "g.edge(\"New Feature\", \"Customer Satisfaction\"),\n",
    "g.edge(\"Unknown Stuff\", \"Customer Satisfaction\"),\n",
    "g.edge(\"Unknown Stuff\", \"Time in App\"),\n",
    "g.edge(\"Time in App\", \"Response\"),\n",
    "g.edge(\"New Feature\", \"Response\"),\n",
    "\n",
    "g.node(\"Response\", \"Response\", color=\"lightgrey\", style=\"filled\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:33.077565Z",
     "start_time": "2023-06-25T15:09:32.864909Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"422pt\" height=\"266pt\"\n",
       " viewBox=\"0.00 0.00 422.00 266.27\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 262.27)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-262.27 418,-262.27 418,4 -4,4\"/>\n",
       "<!-- X1 -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>X1</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-173.27\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-169.57\" font-family=\"Times,serif\" font-size=\"14.00\">X1</text>\n",
       "</g>\n",
       "<!-- U -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>U</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"117\" cy=\"-173.27\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"117\" y=\"-169.57\" font-family=\"Times,serif\" font-size=\"14.00\">U</text>\n",
       "</g>\n",
       "<!-- X1&#45;&gt;U -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>X1&#45;&gt;U</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M54.4,-173.27C62.39,-173.27 71.31,-173.27 79.82,-173.27\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"79.92,-176.77 89.92,-173.27 79.92,-169.77 79.92,-176.77\"/>\n",
       "</g>\n",
       "<!-- X2 -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>X2</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"207\" cy=\"-240.27\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"207\" y=\"-236.57\" font-family=\"Times,serif\" font-size=\"14.00\">X2</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;X2 -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>U&#45;&gt;X2</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M135.39,-186.51C148.28,-196.33 166.07,-209.87 180.56,-220.9\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"178.57,-223.79 188.65,-227.06 182.81,-218.22 178.57,-223.79\"/>\n",
       "</g>\n",
       "<!-- S -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>S</title>\n",
       "<ellipse fill=\"lightgrey\" stroke=\"lightgrey\" cx=\"387\" cy=\"-94.27\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"387\" y=\"-90.57\" font-family=\"Times,serif\" font-size=\"14.00\">S</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;S -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>U&#45;&gt;S</title>\n",
       "<path fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" d=\"M142.08,-180.16C182.2,-190.25 264.19,-204.81 324,-175.27 346.82,-164 363.56,-139.86 373.85,-120.84\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"377.07,-122.23 378.51,-111.73 370.84,-119.04 377.07,-122.23\"/>\n",
       "</g>\n",
       "<!-- Y -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"207\" cy=\"-94.27\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"207\" y=\"-90.57\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;Y -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>U&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" d=\"M134.14,-158.78C147.75,-146.57 167.39,-128.93 182.7,-115.19\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"185.15,-117.69 190.26,-108.4 180.48,-112.48 185.15,-117.69\"/>\n",
       "</g>\n",
       "<!-- X3 -->\n",
       "<g id=\"node7\" class=\"node\">\n",
       "<title>X3</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"297\" cy=\"-148.27\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"297\" y=\"-144.57\" font-family=\"Times,serif\" font-size=\"14.00\">X3</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;X3 -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>U&#45;&gt;X3</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M143.5,-169.69C174.08,-165.39 225.53,-158.17 260.11,-153.31\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"260.84,-156.74 270.26,-151.89 259.87,-149.81 260.84,-156.74\"/>\n",
       "</g>\n",
       "<!-- X5 -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>X5</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"297\" cy=\"-40.27\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"297\" y=\"-36.57\" font-family=\"Times,serif\" font-size=\"14.00\">X5</text>\n",
       "</g>\n",
       "<!-- X5&#45;&gt;S -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>X5&#45;&gt;S</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M317.53,-52.26C329.38,-59.53 344.7,-68.93 357.84,-76.99\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"356.17,-80.07 366.52,-82.32 359.83,-74.11 356.17,-80.07\"/>\n",
       "</g>\n",
       "<!-- X4 -->\n",
       "<g id=\"node8\" class=\"node\">\n",
       "<title>X4</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"297\" cy=\"-94.27\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"297\" y=\"-90.57\" font-family=\"Times,serif\" font-size=\"14.00\">X4</text>\n",
       "</g>\n",
       "<!-- Y&#45;&gt;X4 -->\n",
       "<g id=\"edge8\" class=\"edge\">\n",
       "<title>Y&#45;&gt;X4</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M234.4,-94.27C242.39,-94.27 251.31,-94.27 259.82,-94.27\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"259.92,-97.77 269.92,-94.27 259.92,-90.77 259.92,-97.77\"/>\n",
       "</g>\n",
       "<!-- X3&#45;&gt;S -->\n",
       "<g id=\"edge7\" class=\"edge\">\n",
       "<title>X3&#45;&gt;S</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M317.53,-136.29C329.38,-129.02 344.7,-119.62 357.84,-111.55\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"359.83,-114.44 366.52,-106.22 356.17,-108.47 359.83,-114.44\"/>\n",
       "</g>\n",
       "<!-- X4&#45;&gt;S -->\n",
       "<g id=\"edge9\" class=\"edge\">\n",
       "<title>X4&#45;&gt;S</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M324.4,-94.27C332.39,-94.27 341.31,-94.27 349.82,-94.27\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"349.92,-97.77 359.92,-94.27 349.92,-90.77 349.92,-97.77\"/>\n",
       "</g>\n",
       "<!-- T -->\n",
       "<g id=\"node9\" class=\"node\">\n",
       "<title>T</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"117\" cy=\"-40.27\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"117\" y=\"-36.57\" font-family=\"Times,serif\" font-size=\"14.00\">T</text>\n",
       "</g>\n",
       "<!-- T&#45;&gt;X5 -->\n",
       "<g id=\"edge10\" class=\"edge\">\n",
       "<title>T&#45;&gt;X5</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M144.26,-40.27C174.85,-40.27 225.55,-40.27 259.84,-40.27\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"259.92,-43.77 269.92,-40.27 259.92,-36.77 259.92,-43.77\"/>\n",
       "</g>\n",
       "<!-- T&#45;&gt;S -->\n",
       "<g id=\"edge12\" class=\"edge\">\n",
       "<title>T&#45;&gt;S</title>\n",
       "<path fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" d=\"M139.46,-29.9C178.4,-12.65 262.39,17.16 324,-13.27 346.82,-24.54 363.56,-48.69 373.85,-67.7\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"370.84,-69.5 378.51,-76.81 377.07,-66.31 370.84,-69.5\"/>\n",
       "</g>\n",
       "<!-- T&#45;&gt;Y -->\n",
       "<g id=\"edge11\" class=\"edge\">\n",
       "<title>T&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M137.53,-52.26C149.38,-59.53 164.7,-68.93 177.84,-76.99\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"176.17,-80.07 186.52,-82.32 179.83,-74.11 176.17,-80.07\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f5ae6e0d0>"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph(graph_attr={\"rankdir\": \"LR\"})\n",
    "\n",
    "g.edge(\"X1\", \"U\")\n",
    "g.edge(\"U\", \"X2\")\n",
    "g.edge(\"X5\", \"S\")\n",
    "g.edge(\"U\", \"Y\",style=\"dashed\")\n",
    "g.edge(\"U\", \"S\",style=\"dashed\")\n",
    "g.edge(\"U\", \"X3\")\n",
    "g.edge(\"X3\", \"S\")\n",
    "g.edge(\"Y\", \"X4\")\n",
    "g.edge(\"X4\", \"S\")\n",
    "g.edge(\"T\", \"X5\")\n",
    "g.edge(\"T\", \"Y\")\n",
    "g.edge(\"T\", \"S\", style=\"dashed\")\n",
    "g.node(\"S\", color=\"lightgrey\", style=\"filled\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:33.083084Z",
     "start_time": "2023-06-25T15:09:33.079557Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [],
   "source": [
    "g = gr.Digraph(graph_attr={\"rankdir\":\"LR\"})\n",
    "\n",
    "g.edge(\"Y\", \"X\")\n",
    "g.edge(\"T\", \"X\")\n",
    "g.edge(\"T\", \"Y\")\n",
    "\n",
    "g;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Conditioning on a Mediator\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:33.283592Z",
     "start_time": "2023-06-25T15:09:33.084471Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"301pt\" height=\"166pt\"\n",
       " viewBox=\"0.00 0.00 300.98 166.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 162)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-162 296.98,-162 296.98,4 -4,4\"/>\n",
       "<!-- T -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>T</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"37.05\" cy=\"-34\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"37.05\" y=\"-30.3\" font-family=\"Times,serif\" font-size=\"14.00\">T</text>\n",
       "</g>\n",
       "<!-- M -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>M</title>\n",
       "<ellipse fill=\"lightgrey\" stroke=\"lightgrey\" cx=\"151.69\" cy=\"-41\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"151.69\" y=\"-37.3\" font-family=\"Times,serif\" font-size=\"14.00\">M</text>\n",
       "</g>\n",
       "<!-- T&#45;&gt;M -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>T&#45;&gt;M</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M64.17,-35.62C79.1,-36.55 98.05,-37.73 114.4,-38.74\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"114.51,-42.26 124.71,-39.39 114.94,-35.27 114.51,-42.26\"/>\n",
       "</g>\n",
       "<!-- Y -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"261.13\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"261.13\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- T&#45;&gt;Y -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>T&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M61.4,-25.79C75.34,-21.32 93.5,-16.26 110.09,-14 148.9,-8.71 193.85,-11.01 224.36,-13.81\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"224.22,-17.32 234.51,-14.81 224.9,-10.35 224.22,-17.32\"/>\n",
       "</g>\n",
       "<!-- M&#45;&gt;Y -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>M&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M177.61,-35.67C191.75,-32.64 209.66,-28.8 225.18,-25.48\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"225.93,-28.9 234.98,-23.39 224.47,-22.06 225.93,-28.9\"/>\n",
       "</g>\n",
       "<!-- woman -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>woman</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"37.05\" cy=\"-107\" rx=\"37.09\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"37.05\" y=\"-103.3\" font-family=\"Times,serif\" font-size=\"14.00\">woman</text>\n",
       "</g>\n",
       "<!-- seniority -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>seniority</title>\n",
       "<ellipse fill=\"lightgrey\" stroke=\"lightgrey\" cx=\"151.69\" cy=\"-140\" rx=\"41.69\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"151.69\" y=\"-136.3\" font-family=\"Times,serif\" font-size=\"14.00\">seniority</text>\n",
       "</g>\n",
       "<!-- woman&#45;&gt;seniority -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>woman&#45;&gt;seniority</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M69.13,-116.1C80.74,-119.51 94.16,-123.44 106.71,-127.12\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"105.97,-130.55 116.55,-130 107.94,-123.83 105.97,-130.55\"/>\n",
       "</g>\n",
       "<!-- salary -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>salary</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"261.13\" cy=\"-117\" rx=\"31.7\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"261.13\" y=\"-113.3\" font-family=\"Times,serif\" font-size=\"14.00\">salary</text>\n",
       "</g>\n",
       "<!-- woman&#45;&gt;salary -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>woman&#45;&gt;salary</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M74.13,-108.18C105.6,-109.26 152.47,-110.99 193.29,-113 201.62,-113.41 210.54,-113.91 219.06,-114.41\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"219,-117.91 229.19,-115.01 219.42,-110.92 219,-117.91\"/>\n",
       "</g>\n",
       "<!-- seniority&#45;&gt;salary -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>seniority&#45;&gt;salary</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M189.37,-132.15C199.58,-129.96 210.71,-127.58 221.05,-125.37\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"222.03,-128.74 231.08,-123.22 220.56,-121.89 222.03,-128.74\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f5ae6eb10>"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph(graph_attr={\"rankdir\": \"LR\"})\n",
    "g.edge(\"T\", \"M\")\n",
    "g.edge(\"T\", \"Y\")\n",
    "g.edge(\"M\", \"Y\")\n",
    "g.node(\"M\", color=\"lightgrey\", style=\"filled\")\n",
    "\n",
    "g.edge(\"woman\", \"seniority\")\n",
    "g.edge(\"woman\", \"salary\")\n",
    "g.edge(\"seniority\", \"salary\")\n",
    "g.node(\"seniority\", color=\"lightgrey\", style=\"filled\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:33.488061Z",
     "start_time": "2023-06-25T15:09:33.285736Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"242pt\" height=\"98pt\"\n",
       " viewBox=\"0.00 0.00 242.00 98.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 94)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-94 238,-94 238,4 -4,4\"/>\n",
       "<!-- T -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>T</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-49\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-45.3\" font-family=\"Times,serif\" font-size=\"14.00\">T</text>\n",
       "</g>\n",
       "<!-- M -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>M</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"117\" cy=\"-26\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"117\" y=\"-22.3\" font-family=\"Times,serif\" font-size=\"14.00\">M</text>\n",
       "</g>\n",
       "<!-- T&#45;&gt;M -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>T&#45;&gt;M</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M52.51,-42.59C61.57,-40.23 72.01,-37.5 81.78,-34.95\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"82.83,-38.29 91.62,-32.37 81.06,-31.51 82.83,-38.29\"/>\n",
       "</g>\n",
       "<!-- Y -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"207\" cy=\"-72\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"207\" y=\"-68.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- T&#45;&gt;Y -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>T&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M53.88,-52.34C84.55,-56.31 135.83,-62.93 170.27,-67.38\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"170.01,-70.88 180.37,-68.69 170.91,-63.94 170.01,-70.88\"/>\n",
       "</g>\n",
       "<!-- M&#45;&gt;Y -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>M&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M138.85,-36.9C150.03,-42.74 164,-50.04 176.29,-56.47\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"174.81,-59.65 185.29,-61.18 178.05,-53.44 174.81,-59.65\"/>\n",
       "</g>\n",
       "<!-- X -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>X</title>\n",
       "<ellipse fill=\"lightgrey\" stroke=\"lightgrey\" cx=\"207\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"207\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">X</text>\n",
       "</g>\n",
       "<!-- M&#45;&gt;X -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>M&#45;&gt;X</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M143.93,-23.64C152.09,-22.9 161.26,-22.07 169.99,-21.27\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"170.31,-24.76 179.95,-20.37 169.68,-17.79 170.31,-24.76\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f5ae6ead0>"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph(graph_attr={\"rankdir\": \"LR\"})\n",
    "g.edge(\"T\", \"M\")\n",
    "g.edge(\"T\", \"Y\")\n",
    "g.edge(\"M\", \"Y\")\n",
    "g.edge(\"M\", \"X\")\n",
    "g.node(\"X\", color=\"lightgrey\", style=\"filled\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Key Ideas\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:33.691694Z",
     "start_time": "2023-06-25T15:09:33.489722Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"242pt\" height=\"78pt\"\n",
       " viewBox=\"0.00 0.00 242.00 78.20\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 74.2)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-74.2 238,-74.2 238,4 -4,4\"/>\n",
       "<!-- U -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>U</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-21.05\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-17.35\" font-family=\"Times,serif\" font-size=\"14.00\">U</text>\n",
       "</g>\n",
       "<!-- T -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>T</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"117\" cy=\"-49.05\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"117\" y=\"-45.35\" font-family=\"Times,serif\" font-size=\"14.00\">T</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;T -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>U&#45;&gt;T</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M51.58,-28.55C61.15,-31.6 72.37,-35.17 82.75,-38.47\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"81.76,-41.83 92.35,-41.53 83.88,-35.16 81.76,-41.83\"/>\n",
       "</g>\n",
       "<!-- Y -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"207\" cy=\"-21.05\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"207\" y=\"-17.35\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;Y -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>U&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M54.26,-21.05C84.85,-21.05 135.55,-21.05 169.84,-21.05\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"169.92,-24.55 179.92,-21.05 169.92,-17.55 169.92,-24.55\"/>\n",
       "</g>\n",
       "<!-- T&#45;&gt;Y -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>T&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M141.58,-41.55C151.15,-38.5 162.37,-34.93 172.75,-31.63\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"173.88,-34.94 182.35,-28.58 171.76,-28.27 173.88,-34.94\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f5ae693d0>"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph(graph_attr={\"rankdir\":\"LR\", \"ratio\": \"0.3\"})\n",
    "g.edge(\"U\", \"T\")\n",
    "g.edge(\"U\", \"Y\")\n",
    "g.edge(\"T\", \"Y\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:33.906872Z",
     "start_time": "2023-06-25T15:09:33.693870Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"332pt\" height=\"105pt\"\n",
       " viewBox=\"0.00 0.00 332.00 105.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 101)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-101 328,-101 328,4 -4,4\"/>\n",
       "<!-- T -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>T</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-33\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-29.3\" font-family=\"Times,serif\" font-size=\"14.00\">T</text>\n",
       "</g>\n",
       "<!-- M -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>M</title>\n",
       "<ellipse fill=\"lightgrey\" stroke=\"lightgrey\" cx=\"117\" cy=\"-79\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"117\" y=\"-75.3\" font-family=\"Times,serif\" font-size=\"14.00\">M</text>\n",
       "</g>\n",
       "<!-- T&#45;&gt;M -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>T&#45;&gt;M</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M48.85,-43.9C60.03,-49.74 74,-57.04 86.29,-63.47\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"84.81,-66.65 95.29,-68.18 88.05,-60.44 84.81,-66.65\"/>\n",
       "</g>\n",
       "<!-- Y -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"207\" cy=\"-41\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"207\" y=\"-37.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- T&#45;&gt;Y -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>T&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M54.26,-34.18C84.85,-35.56 135.55,-37.83 169.84,-39.37\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"169.77,-42.88 179.92,-39.83 170.08,-35.88 169.77,-42.88\"/>\n",
       "</g>\n",
       "<!-- S -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>S</title>\n",
       "<ellipse fill=\"lightgrey\" stroke=\"lightgrey\" cx=\"297\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"297\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">S</text>\n",
       "</g>\n",
       "<!-- T&#45;&gt;S -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>T&#45;&gt;S</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M53.46,-28.83C83.58,-24.13 135.28,-16.78 180,-14 203.95,-12.51 210.01,-13.23 234,-14 242.43,-14.27 251.5,-14.75 260.02,-15.28\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"259.87,-18.78 270.08,-15.95 260.33,-11.8 259.87,-18.78\"/>\n",
       "</g>\n",
       "<!-- M&#45;&gt;Y -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>M&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M140.21,-69.41C150.63,-64.91 163.23,-59.47 174.61,-54.56\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"176.01,-57.76 183.8,-50.59 173.23,-51.34 176.01,-57.76\"/>\n",
       "</g>\n",
       "<!-- Y&#45;&gt;S -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>Y&#45;&gt;S</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M232.51,-34.59C241.57,-32.23 252.01,-29.5 261.78,-26.95\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"262.83,-30.29 271.62,-24.37 261.06,-23.51 262.83,-30.29\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f5ae784d0>"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph(graph_attr={\"rankdir\":\"LR\"})\n",
    "g.edge(\"T\", \"M\")\n",
    "g.edge(\"M\", \"Y\")\n",
    "g.edge(\"T\", \"Y\")\n",
    "g.edge(\"T\", \"S\")\n",
    "g.edge(\"Y\", \"S\")\n",
    "\n",
    "g.node(\"M\", color=\"lightgrey\", style=\"filled\")\n",
    "g.node(\"S\", color=\"lightgrey\", style=\"filled\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:34.134806Z",
     "start_time": "2023-06-25T15:09:33.909096Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"469pt\" height=\"67pt\"\n",
       " viewBox=\"0.00 0.00 469.37 67.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 63)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-63 465.37,-63 465.37,4 -4,4\"/>\n",
       "<!-- T -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>T</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">T</text>\n",
       "</g>\n",
       "<!-- In&#45;Game Purchase -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>In&#45;Game Purchase</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"166.69\" cy=\"-41\" rx=\"76.89\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"166.69\" y=\"-37.3\" font-family=\"Times,serif\" font-size=\"14.00\">In&#45;Game Purchase</text>\n",
       "</g>\n",
       "<!-- T&#45;&gt;In&#45;Game Purchase -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>T&#45;&gt;In&#45;Game Purchase</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M53.26,-22.22C64.8,-24.15 79.12,-26.54 93.63,-28.96\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"93.24,-32.45 103.68,-30.64 94.39,-25.54 93.24,-32.45\"/>\n",
       "</g>\n",
       "<!-- In&#45;Game Purchase &gt; 0 -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>In&#45;Game Purchase &gt; 0</title>\n",
       "<ellipse fill=\"lightgrey\" stroke=\"lightgrey\" cx=\"370.38\" cy=\"-18\" rx=\"90.98\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"370.38\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">In&#45;Game Purchase &gt; 0</text>\n",
       "</g>\n",
       "<!-- T&#45;&gt;In&#45;Game Purchase &gt; 0 -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>T&#45;&gt;In&#45;Game Purchase &gt; 0</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M53.92,-15.95C65.03,-15.17 78.14,-14.38 90,-14 150.01,-12.07 217.3,-12.94 270.8,-14.35\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"270.87,-17.86 280.97,-14.63 271.07,-10.86 270.87,-17.86\"/>\n",
       "</g>\n",
       "<!-- In&#45;Game Purchase&#45;&gt;In&#45;Game Purchase &gt; 0 -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>In&#45;Game Purchase&#45;&gt;In&#45;Game Purchase &gt; 0</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M235.97,-33.21C250.37,-31.57 265.75,-29.82 280.77,-28.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"281.55,-31.54 291.09,-26.93 280.75,-24.58 281.55,-31.54\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f5ae78610>"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph(graph_attr={\"rankdir\":\"LR\"})\n",
    "g.edge(\"T\", \"In-Game Purchase\")\n",
    "g.edge(\"T\", \"In-Game Purchase > 0\")\n",
    "g.edge(\"In-Game Purchase\", \"In-Game Purchase > 0\")\n",
    "\n",
    "g.node(\"In-Game Purchase > 0\", color=\"lightgrey\", style=\"filled\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-25T15:09:34.357947Z",
     "start_time": "2023-06-25T15:09:34.136697Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"620pt\" height=\"109pt\"\n",
       " viewBox=\"0.00 0.00 620.36 109.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 105)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-105 616.36,-105 616.36,4 -4,4\"/>\n",
       "<!-- loan amount -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>loan amount</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"55.25\" cy=\"-83\" rx=\"55.49\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"55.25\" y=\"-79.3\" font-family=\"Times,serif\" font-size=\"14.00\">loan amount</text>\n",
       "</g>\n",
       "<!-- Default at yr=1 -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>Default at yr=1</title>\n",
       "<ellipse fill=\"lightgrey\" stroke=\"lightgrey\" cx=\"212.13\" cy=\"-79\" rx=\"65.79\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"212.13\" y=\"-75.3\" font-family=\"Times,serif\" font-size=\"14.00\">Default at yr=1</text>\n",
       "</g>\n",
       "<!-- loan amount&#45;&gt;Default at yr=1 -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>loan amount&#45;&gt;Default at yr=1</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M110.42,-81.6C118.82,-81.38 127.62,-81.16 136.38,-80.93\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"136.71,-84.42 146.62,-80.67 136.53,-77.43 136.71,-84.42\"/>\n",
       "</g>\n",
       "<!-- Default at yr=2 -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>Default at yr=2</title>\n",
       "<ellipse fill=\"darkgrey\" stroke=\"darkgrey\" cx=\"379.42\" cy=\"-41\" rx=\"65.79\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"379.42\" y=\"-37.3\" font-family=\"Times,serif\" font-size=\"14.00\">Default at yr=2</text>\n",
       "</g>\n",
       "<!-- Default at yr=1&#45;&gt;Default at yr=2 -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>Default at yr=1&#45;&gt;Default at yr=2</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M263.17,-67.5C280.65,-63.48 300.45,-58.93 318.64,-54.74\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"319.59,-58.12 328.55,-52.47 318.02,-51.29 319.59,-58.12\"/>\n",
       "</g>\n",
       "<!-- Default at yr=3 -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>Default at yr=3</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"546.71\" cy=\"-18\" rx=\"65.79\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"546.71\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Default at yr=3</text>\n",
       "</g>\n",
       "<!-- Default at yr=2&#45;&gt;Default at yr=3 -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>Default at yr=2&#45;&gt;Default at yr=3</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M438.23,-32.96C451.07,-31.17 464.78,-29.26 477.97,-27.43\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"478.51,-30.88 487.93,-26.04 477.55,-23.95 478.51,-30.88\"/>\n",
       "</g>\n",
       "<!-- U -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>U</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"55.25\" cy=\"-29\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"55.25\" y=\"-25.3\" font-family=\"Times,serif\" font-size=\"14.00\">U</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;Default at yr=1 -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>U&#45;&gt;Default at yr=1</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M79.77,-36.59C100.8,-43.38 132.36,-53.57 159.27,-62.26\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"158.36,-65.64 168.95,-65.38 160.51,-58.98 158.36,-65.64\"/>\n",
       "</g>\n",
       "<!-- U&#45;&gt;Default at yr=2 -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>U&#45;&gt;Default at yr=2</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M82.34,-29.97C130.03,-31.75 232.88,-35.58 303.86,-38.22\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"303.97,-41.73 314.09,-38.6 304.23,-34.73 303.97,-41.73\"/>\n",
       "</g>\n",
       "<!-- U&#45;&gt;Default at yr=3 -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>U&#45;&gt;Default at yr=3</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M82.18,-26.99C128.59,-23.5 228.94,-16.47 313.78,-14 372.1,-12.3 386.73,-12.84 445.07,-14 453.7,-14.17 462.76,-14.43 471.74,-14.72\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"471.72,-18.23 481.83,-15.08 471.96,-11.23 471.72,-18.23\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f9f5ae7ca10>"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph(graph_attr={\"rankdir\": \"LR\"})\n",
    "g.edge(\"loan amount\", \"Default at yr=1\")\n",
    "g.edge(\"Default at yr=1\", \"Default at yr=2\")\n",
    "g.edge(\"Default at yr=2\", \"Default at yr=3\")\n",
    "g.edge(\"U\", \"Default at yr=1\")\n",
    "g.edge(\"U\", \"Default at yr=2\")\n",
    "g.edge(\"U\", \"Default at yr=3\")\n",
    "\n",
    "g.node(\"Default at yr=1\", color=\"lightgrey\", style=\"filled\")\n",
    "g.node(\"Default at yr=2\", color=\"darkgrey\", style=\"filled\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "celltoolbar": "Tags",
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
